fix(core): fix logging when directory does not exist (#30944)

This commit is contained in:
Jason Jean 2025-04-30 14:34:36 -04:00 committed by GitHub
parent 766d1b32e0
commit 7a62c7374b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 9 deletions

View File

@ -1,4 +1,6 @@
use colored::Colorize;
use std::env;
use std::fs::create_dir_all;
use std::io::IsTerminal;
use tracing::{Event, Level, Subscriber};
use tracing_appender::rolling::{RollingFileAppender, Rotation};
@ -101,8 +103,28 @@ pub(crate) fn enable_logger() {
.unwrap_or_else(|_| EnvFilter::new("ERROR")),
);
let registry = tracing_subscriber::registry().with(stdout_layer);
if env::var("NX_NATIVE_FILE_LOGGING").is_err() {
// File logging is not enabled
registry.try_init().ok();
return;
}
let log_dir = ".nx/workspace-data";
if let Err(e) = create_dir_all(log_dir) {
// Could not create the directory, so we will not log to file
println!(
"Logging to a file was not enabled because Nx could not create the {} directory for logging. Error: {}",
log_dir, e
);
registry.try_init().ok();
return;
};
let file_appender: RollingFileAppender =
RollingFileAppender::new(Rotation::NEVER, ".nx/workspace-data", "nx.log");
RollingFileAppender::new(Rotation::NEVER, log_dir, "nx.log");
let file_layer = tracing_subscriber::fmt::layer()
.with_writer(file_appender)
.event_format(NxLogFormatter)
@ -111,9 +133,6 @@ pub(crate) fn enable_logger() {
EnvFilter::try_from_env("NX_NATIVE_FILE_LOGGING")
.unwrap_or_else(|_| EnvFilter::new("ERROR")),
);
tracing_subscriber::registry()
.with(stdout_layer)
.with(file_layer)
.try_init()
.ok();
registry.with(file_layer).try_init().ok();
}

View File

@ -1,5 +1,6 @@
import type { NxJsonConfiguration } from '../config/nx-json';
import { readNxJsonFromDisk } from '../devkit-internals';
import { IS_WASM } from '../native';
import { isCI } from '../utils/is-ci';
let tuiEnabled = undefined;
@ -31,7 +32,7 @@ export function isTuiEnabled(
// Windows is not working well right now, temporarily disable it on Windows even if it has been specified as enabled
// TODO(@JamesHenry): Remove this check once Windows issues are fixed.
if (isCI() || isWindows) {
if (isCI() || isWindows || IS_WASM) {
tuiEnabled = false;
process.env.NX_TUI = 'false';
return tuiEnabled;

View File

@ -10,6 +10,7 @@ import { runCommands } from '../executors/run-commands/run-commands.impl';
import { getTaskDetails, hashTask } from '../hasher/hash-task';
import { TaskHasher } from '../hasher/task-hasher';
import {
IS_WASM,
parseTaskStatus,
RunningTasksService,
TaskDetails,
@ -55,7 +56,9 @@ export class TaskOrchestrator {
this.tuiEnabled
);
private runningTasksService = new RunningTasksService(getDbConnection());
private runningTasksService = !IS_WASM
? new RunningTasksService(getDbConnection())
: null;
private tasksSchedule = new TasksSchedule(
this.projectGraph,
this.taskGraph,
@ -686,7 +689,10 @@ export class TaskOrchestrator {
}
async startContinuousTask(task: Task, groupId: number) {
if (this.runningTasksService.getRunningTasks([task.id]).length) {
if (
this.runningTasksService &&
this.runningTasksService.getRunningTasks([task.id]).length
) {
await this.preRunSteps([task], { groupId });
if (this.tuiEnabled) {