Skip to content

Commit

Permalink
[workspace-resolve] Fix win32 path formatting.
Browse files Browse the repository at this point in the history
Also tap hooks earlier to avoid sequencing issues.
  • Loading branch information
dmichon-msft committed Dec 17, 2024
1 parent 9428b4e commit fc04182
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/webpack-workspace-resolve-plugin",
"comment": "Fix a bug with path handling on Windows. Tap hooks earlier to ensure that these plugins run before builtin behavior.",
"type": "patch"
}
],
"packageName": "@rushstack/webpack-workspace-resolve-plugin"
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ export class KnownPackageDependenciesPlugin {
let scope: IPrefixMatch<IResolveContext> | undefined =
cache.contextForPackage.get(descriptionFileData);
if (!scope) {
return callback(new Error(`Expected context for ${request.descriptionFileRoot}`));
scope = cache.contextLookup.findLongestPrefixMatch(path);
if (!scope) {
return callback(new Error(`Expected context for ${request.descriptionFileRoot}`));
}
cache.contextForPackage.set(descriptionFileData, scope);
}

let dependency: IPrefixMatch<IResolveContext> | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ export class WorkspaceLayoutCache {

public get descriptionFileRoot(): string {
if (!this._descriptionFileRoot) {
this._descriptionFileRoot = `${basePath}${
normalizeToPlatform?.(this._serialized.root) ?? this._serialized.root
}`;
const merged: string = `${basePath}${this._serialized.root}`;
this._descriptionFileRoot = normalizeToPlatform?.(merged) ?? merged;
}
return this._descriptionFileRoot;
}
Expand Down Expand Up @@ -183,18 +182,26 @@ export class WorkspaceLayoutCache {
const resolveContext: ResolveContext = new ResolveContext(serialized);
resolveContexts.push(resolveContext);

const descriptionFileRoot: string = resolveContext.descriptionFileRoot;
contextLookup.setItem(descriptionFileRoot, resolveContext);
contextLookup.setItemFromSegments(
concat<string>(
// All paths in the cache file are platform-agnostic
LookupByPath.iteratePathSegments(basePath, '/'),
LookupByPath.iteratePathSegments(serialized.root, '/')
),
resolveContext
);

// Handle nested package.json files. These may modify some properties, but the dependency resolution
// will match the original package root. Typically these are used to set the `type` field to `module`.
if (serialized.dirInfoFiles) {
for (const file of serialized.dirInfoFiles) {
contextLookup.setItemFromSegments(
concat<string>(
// Root is normalized to platform slashes
LookupByPath.iteratePathSegments(descriptionFileRoot, resolverPathSeparator),
// Subpaths are platform-agnostic
// All paths in the cache file are platform-agnostic
concat<string>(
LookupByPath.iteratePathSegments(basePath, '/'),
LookupByPath.iteratePathSegments(serialized.root, '/')
),
LookupByPath.iteratePathSegments(file, '/')
),
resolveContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,22 @@ export class WorkspaceResolvePlugin implements WebpackPluginInstance {
resolveOptions.plugins ??= [];
resolveOptions.plugins.push(
// Optimize identifying the package.json file for the issuer
new KnownDescriptionFilePlugin(cache, 'parsed-resolve', 'described-resolve'),
new KnownDescriptionFilePlugin(cache, 'before-parsed-resolve', 'described-resolve'),
// Optimize locating the installed dependencies of the current package
new KnownPackageDependenciesPlugin(cache, 'raw-module', 'resolve-as-module'),
new KnownPackageDependenciesPlugin(cache, 'before-raw-module', 'resolve-as-module'),
// Optimize loading the package.json file for the destination package (bare specifier)
new KnownDescriptionFilePlugin(cache, 'resolve-as-module', 'resolve-in-package'),
new KnownDescriptionFilePlugin(cache, 'before-resolve-as-module', 'resolve-in-package'),
// Optimize loading the package.json file for the destination package (relative path)
new KnownDescriptionFilePlugin(cache, 'relative', 'described-relative'),
new KnownDescriptionFilePlugin(cache, 'before-relative', 'described-relative'),
// Optimize locating and loading nested package.json for a directory
new KnownDescriptionFilePlugin(cache, 'undescribed-existing-directory', 'existing-directory', true),
new KnownDescriptionFilePlugin(
cache,
'before-undescribed-existing-directory',
'existing-directory',
true
),
// Optimize locating and loading nested package.json for a file
new KnownDescriptionFilePlugin(cache, 'undescribed-raw-file', 'raw-file')
new KnownDescriptionFilePlugin(cache, 'before-undescribed-raw-file', 'raw-file')
);

return resolveOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function createResolveForTests(

const cache: WorkspaceLayoutCache = new WorkspaceLayoutCache({
cacheData: {
basePath: `${separator}workspace${separator}`,
basePath: `/workspace/`,
contexts: [
{
root: 'a',
Expand Down

0 comments on commit fc04182

Please sign in to comment.