Skip to content

Commit

Permalink
[rush-resolver-cache] Fix projectFolder on Windows (#4931)
Browse files Browse the repository at this point in the history
Co-authored-by: David Michon <[email protected]>
  • Loading branch information
dmichon-msft and dmichon-msft authored Sep 20, 2024
1 parent 9471818 commit 476c61e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Fix a bug that caused rush-resolver-cache-plugin to crash on Windows.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ export interface IComputeResolverCacheFromLockfileOptions {
) => Promise<void>;
}

/**
* Copied from `@rushstack/node-core-library/src/Path.ts` to avoid expensive dependency
* @param path - Path using backslashes as path separators
* @returns The same string using forward slashes as path separators
*/
function convertToSlashes(path: string): string {
return path.replace(/\\/g, '/');
}

/**
* Given a lockfile and information about the workspace and platform, computes the resolver cache file.
* @param params - The options for computing the resolver cache
Expand All @@ -146,9 +155,9 @@ export async function computeResolverCacheFromLockfileAsync(
): Promise<IResolverCacheFile> {
const { platformInfo, projectByImporterPath, lockfile, afterExternalPackagesAsync } = params;
// Needs to be normalized to `/` for path.posix.join to work correctly
const workspaceRoot: string = params.workspaceRoot.replace(/\\/g, '/');
const workspaceRoot: string = convertToSlashes(params.workspaceRoot);
// Needs to be normalized to `/` for path.posix.join to work correctly
const commonPrefixToTrim: string = params.commonPrefixToTrim.replace(/\\/g, '/');
const commonPrefixToTrim: string = convertToSlashes(params.commonPrefixToTrim);

const contexts: Map<string, IResolverContext> = new Map();
const missingOptionalDependencies: Set<string> = new Set();
Expand Down Expand Up @@ -218,16 +227,18 @@ export async function computeResolverCacheFromLockfileAsync(
throw new Error(`Missing project for importer ${importerPath}`);
}

const descriptionFileRoot: string = convertToSlashes(project.projectFolder);

const context: IResolverContext = {
descriptionFileRoot: project.projectFolder,
descriptionFileRoot,
descriptionFileHash: undefined, // Not needed anymore
name: project.packageJson.name,
isProject: true,
deps: new Map(),
ordinal: -1
};

contexts.set(project.projectFolder, context);
contexts.set(descriptionFileRoot, context);

if (importer.dependencies) {
resolveDependencies(workspaceRoot, importer.dependencies, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ describe(computeResolverCacheFromLockfileAsync.name, () => {
for (const importerPath of lockfile.importers.keys()) {
const remainder: string = importerPath.slice(importerPath.lastIndexOf('../') + 3);
projectByImporterPath.setItem(importerPath, {
projectFolder: `${commonPrefixToTrim.replace(/\\/g, '/')}${remainder}`,
// Normalization is the responsibility of the implementation
projectFolder: `${commonPrefixToTrim}${remainder}`,
packageJson: {
name: `@local/${remainder.replace(/\//g, '+')}`
}
Expand Down

0 comments on commit 476c61e

Please sign in to comment.