Skip to content
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

Fix relative path #57961

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 8 additions & 4 deletions src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,8 +548,8 @@ function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOpt
return maybeNonRelative;
}

const nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson(host, getDirectoryPath(modulePath));
const nearestSourcePackageJson = getNearestAncestorDirectoryWithPackageJson(host, sourceDirectory);
const nearestTargetPackageJson = getNearestAncestorDirectoryWithPackageJson(host, getDirectoryPath(modulePath), { returnAsBooleanIfHostMethodMissing: true });
const nearestSourcePackageJson = getNearestAncestorDirectoryWithPackageJson(host, sourceDirectory, { returnAsBooleanIfHostMethodMissing: true });
Comment on lines +551 to +552
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just do !! at the start of these? No other calls need to pass these options in.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just do !! at the start of these? No other calls need to pass these options in.

According to the logic before, if the host.getNearestAncestorDirectoryWithPackageJson methods exist, will use the return value of a string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, didn't realize this returned a string, and the point is to compare those.

I don't think that this is exactly how we'd want to fix it as it seems to overfit for the bug, but without a test case I can't really offer a better idea yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll say that this fix just seems confusing; it's basically changing the code to report maybeNonRelative if the paths have differing "is in a package"-ness, but that sure feels like the wrong signal. But that's what the old code kinda did too. @andrewbranch may have a better clue here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't you just be able to check that getNearestAncestorDirectoryWithPackageJson(host, sourceDirectory) !== undefined?

if (nearestSourcePackageJson !== nearestTargetPackageJson) {
// 2. The importing and imported files are part of different packages.
//
Expand Down Expand Up @@ -583,13 +583,17 @@ function comparePathsByRedirectAndNumberOfDirectorySeparators(a: ModulePath, b:
return compareBooleans(b.isRedirect, a.isRedirect) || compareNumberOfDirectorySeparators(a.path, b.path);
}

function getNearestAncestorDirectoryWithPackageJson(host: ModuleSpecifierResolutionHost, fileName: string) {
function getNearestAncestorDirectoryWithPackageJson(host: ModuleSpecifierResolutionHost, fileName: string, options?: { returnAsBooleanIfHostMethodMissing?: false }): string | undefined;
function getNearestAncestorDirectoryWithPackageJson(host: ModuleSpecifierResolutionHost, fileName: string, options?: { returnAsBooleanIfHostMethodMissing: true }): boolean | string | undefined;
function getNearestAncestorDirectoryWithPackageJson(host: ModuleSpecifierResolutionHost, fileName: string, { returnAsBooleanIfHostMethodMissing }: { returnAsBooleanIfHostMethodMissing?: boolean } = {}) {
if (host.getNearestAncestorDirectoryWithPackageJson) {
return host.getNearestAncestorDirectoryWithPackageJson(fileName);
}
return forEachAncestorDirectory(fileName, directory => {
const result = forEachAncestorDirectory(fileName, directory => {
return host.fileExists(combinePaths(directory, "package.json")) ? directory : undefined;
});

return returnAsBooleanIfHostMethodMissing ? !!result : result;
}

/** @internal */
Expand Down
Loading