Skip to content

[rush-resolver-cache] Fix projectFolder on Windows #4931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading