Skip to content

Commit bb0b1d4

Browse files
authored
fix(compiler): normalize paths on windows (#4997)
* fix(compiler): normalize paths on windows this patch ensures that paths are properly normalized by the compiler. this ensures that regardless of the platform (operating system) that a project is compiled on, paths are uniformly treated internally by stencil. this has system-wide reaching effects - from the in-memory filesystem, to configuration/output target validation, and file generation. previously, stencil's in-browser compilation support included a polyfill for the following NodeJS `path` module functions: `join`, `normalize`, `relative` & `resolve`. this polyfill did the following: - it wrapped each of the aforementioned functions in a `normalizePath` function to convert Windows-style path separators (`\\`) to Unix/POSIX-style path separators (`/`) - it overwrote the standard NodeJS `path` implementations for each of these functions. as a result, calling `join` or any of the other three methods, even when importing the method from `path` like below would result in the polyfill being called: ```ts import { join } from 'path'; // this imports the polyfilled `join` // runs the native `path.join`, then normalizes the returned path const filePath = join(part1, part2); ``` while this was 'nice' in that stencil engineers didn't need to think about which implementation of `path` functions they were using, this polyfill made some behavior of the compiler hard to understand. the polyfills were removed in #4317 (b042d8b). this led to calls to the aforementioned functions to call their original implementations, rather than the wrapped implementations: ```ts import { join } from 'path'; // imports Node's `join` // run the native `path.join`, without any normalization const filePath = join(part1, part2); ``` discrepencies arose where parts of the code would explicitly wrap a call to `join()` (or one of its ilk) around a path normalization function. this caused paths to not be uniformly normalized throughout the codebase, leading to errors. since the removal of in-browser compilation, additional pull requests to fix path-related issues on windows have landed in the codebase: - #4545 (cd58d9c) - #4932 (b97dadc) this commit builds on the previous commits by attempting to move stencil's compiler completely over to polyfilled versions of the mentioned `path` functions that are explicitly imported/called. Some of the changes found herein were created/validated using Alice's codemod branch, #4996 Co-authored-by: alicewriteswrongs <alicewriteswrongs@users.noreply.github.com> STENCIL-975 Determine Scope of Path Polyfill Bug, Fix Fixes: #4980 Fixes: #4961 Spun Off: #5036, #5032, #5029 * fix validate-dist-collection tests * fix prerendered-write-path test * fix stencil-types tests this commit removes a spy on `join.resolve` and updates the mocks to properly spy on the wrapped `@utils`' `resolve` fn * fix validate-paths tests this commit updates the tests for `validate-paths`. it replaces instances of `path.join` in assertions with stencil's own `join` function. existing instances of `path.join` have been left as-is if they pertain to user input. this allows us to exercise a user using either path separator in CI (which runs in windows and linux). the cache directory is now normalized if it is absolute. otherwise, provided cache directories in `stencil.config.ts` would never get normalized * fix validate-output-www tests this commit updates the tests for `validate-paths`. it replaces instances of `path.join` in assertions with stencil's own `join` function. existing instances of `path.join` have been left as-is if they pertain to user input. this allows us to exercise a user using either path separator in CI (which runs in windows and linux). * fix validate-output-dist tests this commit updates the tests for `validate-output`. it replaces instances of `path.join` in assertions with stencil's own `join` function. existing instances of `path.join` have been left as-is if they pertain to user input. this allows us to exercise a user using either path separator in CI (which runs in windows and linux). this commit also switches `output-target.ts#getcomponentsDtsSrcFilePath` over to use Stencil's join function to fix the tests * fix validate-output-dist-custom-element tests this commit updates the tests for `validate-output-dist-custom-element`. it replaces instances of `path.join` in assertions with stencil's own `join function. existing instances of `path.join` have been left as-is if they pertain to user input. this allows us to exercise a user using either path separator in CI (which runs in windows and linux). * fix validate-testing tests this commit updates the tests for `validate-testing`. it replaces instances of `path.join` in assertions with stencil's own `join` function. existing instances of `path.join` have been left as-is if they pertain to user input. this allows us to exercise a user using either path separator in CI (which runs in windows and linux). the screenshot connector is now normalized if it is absolute. otherwise, provided connector file path in `stencil.config.ts` would never get normalized
1 parent bd826ff commit bb0b1d4

82 files changed

Lines changed: 345 additions & 275 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/compiler/app-core/app-es5-disabled.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { escapeHtml, generatePreamble } from '@utils';
2-
import { join } from 'path';
1+
import { escapeHtml, generatePreamble, join } from '@utils';
32

43
import type * as d from '../../declarations';
54

src/compiler/app-core/app-polyfills.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'path';
1+
import { join } from '@utils';
22

33
import type * as d from '../../declarations';
44

src/compiler/build/compiler-ctx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { noop, normalizePath } from '@utils';
2-
import { basename, dirname, extname, join } from 'path';
1+
import { join, noop, normalizePath } from '@utils';
2+
import { basename, dirname, extname } from 'path';
33

44
import type * as d from '../../declarations';
55
import { buildEvents } from '../events';

src/compiler/build/watch-build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { isString } from '@utils';
2-
import { dirname, resolve } from 'path';
1+
import { isString, resolve } from '@utils';
2+
import { dirname } from 'path';
33
import type ts from 'typescript';
44

55
import type * as d from '../../declarations';

src/compiler/bundle/core-resolve-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { isRemoteUrl, normalizeFsPath, normalizePath } from '@utils';
2-
import { dirname, join } from 'path';
1+
import { isRemoteUrl, join, normalizeFsPath, normalizePath } from '@utils';
2+
import { dirname } from 'path';
33
import type { Plugin } from 'rollup';
44

55
import type * as d from '../../declarations';

src/compiler/bundle/dev-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { generatePreamble } from '@utils';
2-
import { basename, dirname, join, relative } from 'path';
1+
import { generatePreamble, join, relative } from '@utils';
2+
import { basename, dirname } from 'path';
33
import { OutputOptions, rollup } from 'rollup';
44

55
import type * as d from '../../declarations';

src/compiler/bundle/dev-node-module-resolve.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { basename, dirname, join, relative } from 'path';
1+
import { join, relative } from '@utils';
2+
import { basename, dirname } from 'path';
23
import { ResolveIdResult } from 'rollup';
34

45
import type * as d from '../../declarations';

src/compiler/bundle/ext-transforms-plugin.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { hasError, isOutputTargetDistCollection, normalizeFsPath } from '@utils';
2-
import { join, relative } from 'path';
1+
import { hasError, isOutputTargetDistCollection, join, normalizeFsPath, relative } from '@utils';
32
import type { Plugin } from 'rollup';
43

54
import type * as d from '../../declarations';

src/compiler/bundle/plugin-helper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { buildError } from '@utils';
2-
import { relative } from 'path';
1+
import { buildError, relative } from '@utils';
32

43
import type * as d from '../../declarations';
54

src/compiler/bundle/user-index-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'path';
1+
import { join } from '@utils';
22
import type { Plugin } from 'rollup';
33

44
import type * as d from '../../declarations';

0 commit comments

Comments
 (0)