From ea38c1d4c66dd0dcb4df69d7203e727a49ce375b Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 25 Jul 2024 15:10:40 -0700 Subject: [PATCH 1/2] Warn when using windows style path separator --- packages/compiler/src/config/config-loader.ts | 7 ++++ packages/compiler/src/core/messages.ts | 6 ++++ .../compiler/src/core/schema-validator.ts | 34 ++++++++++++++----- .../test/core/emitter-options.test.ts | 10 ++++++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/packages/compiler/src/config/config-loader.ts b/packages/compiler/src/config/config-loader.ts index a1f66039f90..be487838caa 100644 --- a/packages/compiler/src/config/config-loader.ts +++ b/packages/compiler/src/config/config-loader.ts @@ -260,6 +260,13 @@ function validatePathAbsolute( target: target === NoTarget ? target : getLocationInYamlScript(target.file, target.path), }); } + if (path.includes("\\")) { + return createDiagnostic({ + code: "path-unix-style", + format: { path }, + target: target === NoTarget ? target : getLocationInYamlScript(target.file, target.path), + }); + } return undefined; } diff --git a/packages/compiler/src/core/messages.ts b/packages/compiler/src/core/messages.ts index 025d50a5ef0..f7cbcedd848 100644 --- a/packages/compiler/src/core/messages.ts +++ b/packages/compiler/src/core/messages.ts @@ -606,6 +606,12 @@ const diagnostics = { default: paramMessage`Path "${"path"}" cannot be relative. Use {cwd} or {project-root} to specify what the path should be relative to.`, }, }, + "path-unix-style": { + severity: "warning", + messages: { + default: paramMessage`Path should use unix style separators. Use "/" instead of "\\".`, + }, + }, "config-path-not-found": { severity: "error", messages: { diff --git a/packages/compiler/src/core/schema-validator.ts b/packages/compiler/src/core/schema-validator.ts index cf875c89c07..4c3a2894c15 100644 --- a/packages/compiler/src/core/schema-validator.ts +++ b/packages/compiler/src/core/schema-validator.ts @@ -18,6 +18,16 @@ export interface JSONSchemaValidatorOptions { strict?: boolean; } +function absolutePathStatus(path: string): "valid" | "not-absolute" | "windows-style" { + if (path.startsWith(".") || !isPathAbsolute(path)) { + return "not-absolute"; + } + if (path.includes("\\")) { + return "windows-style"; + } + return "valid"; +} + export function createJSONSchemaValidator( schema: JSONSchemaType, options: JSONSchemaValidatorOptions = { strict: true } @@ -30,9 +40,7 @@ export function createJSONSchemaValidator( ajv.addFormat("absolute-path", { type: "string", - validate: (path) => { - return !path.startsWith(".") && isPathAbsolute(path); - }, + validate: (path) => absolutePathStatus(path) === "valid", }); return { validate }; @@ -66,11 +74,21 @@ function ajvErrorToDiagnostic( ): Diagnostic { const tspTarget = resolveTarget(error, target); if (error.params.format === "absolute-path") { - return createDiagnostic({ - code: "config-path-absolute", - format: { path: getErrorValue(obj, error) as any }, - target: tspTarget, - }); + const value = getErrorValue(obj, error) as any; + const status = absolutePathStatus(value); + if (status === "windows-style") { + return createDiagnostic({ + code: "path-unix-style", + format: { path: value }, + target: tspTarget, + }); + } else { + return createDiagnostic({ + code: "config-path-absolute", + format: { path: value }, + target: tspTarget, + }); + } } const messageLines = [`Schema violation: ${error.message} (${error.instancePath || "/"})`]; diff --git a/packages/compiler/test/core/emitter-options.test.ts b/packages/compiler/test/core/emitter-options.test.ts index b0c516505a3..b4ea88c8d9b 100644 --- a/packages/compiler/test/core/emitter-options.test.ts +++ b/packages/compiler/test/core/emitter-options.test.ts @@ -117,5 +117,15 @@ describe("compiler: emitter options", () => { message: `Path "assets" cannot be relative. Use {cwd} or {project-root} to specify what the path should be relative to.`, }); }); + + it("emit diagnostic if passing windows style path", async () => { + const diagnostics = await diagnoseEmitterOptions({ + "asset-dir": "C:\\abc\\def", + }); + expectDiagnostics(diagnostics, { + code: "path-unix-style", + message: `Path should use unix style separators. Use "/" instead of "\\".`, + }); + }); }); }); From 2cd93195137ccd973037937383478efce01fae09 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Thu, 25 Jul 2024 15:13:21 -0700 Subject: [PATCH 2/2] Chanmgelog --- .chronus/changes/path-unix-style-2024-6-25-15-13-16.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/path-unix-style-2024-6-25-15-13-16.md diff --git a/.chronus/changes/path-unix-style-2024-6-25-15-13-16.md b/.chronus/changes/path-unix-style-2024-6-25-15-13-16.md new file mode 100644 index 00000000000..ab5446159c7 --- /dev/null +++ b/.chronus/changes/path-unix-style-2024-6-25-15-13-16.md @@ -0,0 +1,7 @@ +--- +changeKind: feature +packages: + - "@typespec/compiler" +--- + +Warn when using `\` in config file field that expect a path. \ No newline at end of file