Skip to content

Commit de6b171

Browse files
authored
[wrangler] Lazily resolve WRANGLER_LOG_PATH on first write (#16947)
1 parent 1af3c3a commit de6b171

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix `WRANGLER_LOG_PATH` being ignored when set in `vite.config.ts` after a static import of `@cloudflare/vite-plugin`
6+
7+
The debug log filepath is now resolved lazily on first write (memoized), matching how `WRANGLER_WRITE_LOGS` is read. Setting `process.env.WRANGLER_LOG_PATH` in the Vite config body after importing the plugin now writes logs to the configured path instead of the global default directory.

‎packages/wrangler/src/__tests__/utils/log-file.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import { beforeEach, describe, it, vi } from "vitest";
1212
import {
1313
appendToDebugLogFile,
1414
cleanupOldLogFiles,
15-
debugLogFilepath,
15+
getDebugLogFilepath,
16+
resetDebugLogFilepathForTests,
1617
tryCleanupLogs,
1718
} from "../../utils/log-file";
1819
import type { ExpectStatic } from "vitest";
@@ -21,10 +22,12 @@ describe("appendToDebugLogFile", () => {
2122
runInTempDir();
2223

2324
beforeEach(() => {
25+
resetDebugLogFilepathForTests();
2426
vi.stubEnv("WRANGLER_LOG_PATH", "logs");
2527
});
2628

2729
function getLogFileContent(expect: ExpectStatic): string {
30+
const debugLogFilepath = getDebugLogFilepath();
2831
if (existsSync(debugLogFilepath)) {
2932
return readFileSync(debugLogFilepath, "utf8");
3033
}
@@ -206,6 +209,7 @@ describe("tryCleanupLogs", () => {
206209
runInTempDir();
207210

208211
beforeEach(() => {
212+
resetDebugLogFilepathForTests();
209213
vi.stubEnv("WRANGLER_LOG_PATH", "logs");
210214
});
211215

@@ -221,3 +225,27 @@ describe("tryCleanupLogs", () => {
221225
expect(() => tryCleanupLogs()).not.toThrow();
222226
});
223227
});
228+
229+
describe("getDebugLogFilepath lazy resolution", () => {
230+
runInTempDir();
231+
232+
beforeEach(() => {
233+
resetDebugLogFilepathForTests();
234+
});
235+
236+
it("should respect WRANGLER_LOG_PATH set after the module has already been imported", async ({
237+
expect,
238+
}) => {
239+
// Module is already loaded via the static import at the top of this
240+
// file — same as `import { cloudflare } from "@cloudflare/vite-plugin"`
241+
// evaluating wrangler before the vite.config.ts body runs.
242+
vi.stubEnv("WRANGLER_LOG_PATH", "after-import-logs");
243+
244+
await appendToDebugLogFile("info", "lazy path test");
245+
246+
const filepath = getDebugLogFilepath();
247+
expect(filepath).toContain("after-import-logs");
248+
expect(existsSync(filepath)).toBe(true);
249+
expect(readFileSync(filepath, "utf8")).toContain("lazy path test");
250+
});
251+
});

‎packages/wrangler/src/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ import {
512512
} from "./user/profiles";
513513
import { noProxy, proxy } from "./utils/constants";
514514
import { logDidYouMean } from "./utils/did-you-mean";
515-
import { debugLogFilepath } from "./utils/log-file";
515+
import { getDebugLogFilepath } from "./utils/log-file";
516516
import { vectorizeCreateCommand } from "./vectorize/create";
517517
import { vectorizeCreateMetadataIndexCommand } from "./vectorize/createMetadataIndex";
518518
import { vectorizeDeleteCommand } from "./vectorize/delete";
@@ -713,7 +713,7 @@ export function createCLIParser(argv: string[]) {
713713
version: 1,
714714
wrangler_version: wranglerVersion,
715715
command_line_args: redactEventCode(argv),
716-
log_file_path: debugLogFilepath,
716+
log_file_path: getDebugLogFilepath(),
717717
});
718718

719719
return true;

‎packages/wrangler/src/utils/log-file.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,23 @@ export async function cleanupOldLogFiles(logsDir: string): Promise<void> {
8787
}
8888
}
8989

90-
export const debugLogFilepath = getDebugFilepath();
90+
let cachedDebugLogFilepath: string | undefined;
91+
92+
/**
93+
* Returns the memoized debug log filepath, resolving `WRANGLER_LOG_PATH` on
94+
* first access so values set after module import — e.g. in `vite.config.ts`
95+
* after a static import of `@cloudflare/vite-plugin` — are still respected.
96+
* Matches the lazy read of `WRANGLER_WRITE_LOGS` in `shouldLogToDisk()`.
97+
*/
98+
export function getDebugLogFilepath(): string {
99+
return (cachedDebugLogFilepath ??= getDebugFilepath());
100+
}
101+
102+
/** Clears the memoized log path. Used by tests that change cwd or env between cases. */
103+
export function resetDebugLogFilepathForTests(): void {
104+
cachedDebugLogFilepath = undefined;
105+
}
106+
91107
const mutex = new Mutex();
92108

93109
let hasStartedLogCleanup = false;
@@ -114,6 +130,8 @@ export async function appendToDebugLogFile(
114130
) {
115131
tryCleanupLogs();
116132

133+
const debugLogFilepath = getDebugLogFilepath();
134+
117135
const entry = `
118136
--- ${new Date().toISOString()} ${messageLevel}
119137
${stripVTControlCharacters(message)}

0 commit comments

Comments
 (0)