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 rename cause import change wrongly #38462

Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/compiler/moduleSpecifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ namespace ts.moduleSpecifiers {
// Get a path that's relative to node_modules or the importing file's path
// if node_modules folder is in this folder or any of its parent folders, no need to keep it.
const pathToTopLevelNodeModules = getCanonicalFileName(moduleSpecifier.substring(0, parts.topLevelNodeModulesIndex));
if (!(startsWith(sourceDirectory, pathToTopLevelNodeModules) || globalTypingsCacheLocation && startsWith(getCanonicalFileName(globalTypingsCacheLocation), pathToTopLevelNodeModules))) {
const canonicalSourceDirectory = getCanonicalFileName(sourceDirectory);
if (!(startsWith(canonicalSourceDirectory, pathToTopLevelNodeModules) || globalTypingsCacheLocation && startsWith(getCanonicalFileName(globalTypingsCacheLocation), pathToTopLevelNodeModules))) {
Copy link
Member

Choose a reason for hiding this comment

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

Wait this seems incorrect. sourceDirectory is already canonical path.
https://github.com/microsoft/TypeScript/pull/38462/files#diff-aa86ac6315ba3e1d25a9a1789f7f5a78L110

Copy link
Member

Choose a reason for hiding this comment

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

Since this is the only substantive change outside of tests in this PR, is there something we are doing incorrectly when computing sourceDirectory? If not, what does this PR change other than the VFS in the test harness and adding a test?

Copy link
Member

Choose a reason for hiding this comment

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

I think we would need to diagnose why source directory is not canonical already instead.

Copy link
Contributor Author

@ShuiRuTian ShuiRuTian May 22, 2020

Choose a reason for hiding this comment

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

#38462 (comment)
Would it be better to add a comment to let users know it is their responsibility to make sure sourceDirectory canonical path?

return undefined;
}

Expand Down
5 changes: 4 additions & 1 deletion src/harness/vfsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ namespace vfs {
if (existingNode) {
if (isDirectory(node)) {
if (!isDirectory(existingNode)) throw createIOError("ENOTDIR");
if (this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY");
// if both old and new arguments point to the same directory, just pass. So we could rename /src/a/1 to /src/A/1 in Win.
// if not and the directory pointed by the new path is not empty, throw an error.
// However, in the link in the description, it says: If the old argument points to the pathname of a file that is not a directory, the new argument shall not point to the pathname of a directory. If the link named by the new argument exists, it shall be removed and old renamed to new. In this case, a link named new shall remain visible to other threads throughout the renaming operation and refer either to the file referred to by new or old before the operation began. Write access permission is required for both the directory containing old and the directory containing new.
Copy link
Member

Choose a reason for hiding this comment

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

This comment is too long to read easily in a PR. Please split this comment across multiple lines.

Copy link
Contributor Author

@ShuiRuTian ShuiRuTian May 22, 2020

Choose a reason for hiding this comment

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

Remove it and the content is as follows.

In the link http://pubs.opengroup.org/onlinepubs/9699919799/functions/rename.html
It says
If the old argument points to the pathname of a file that is not a directory, the new argument shall not point to the pathname of a directory. If the link named by the new argument exists, it shall be removed and old renamed to new. In this case, a link named new shall remain visible to other threads throughout the renaming operation and refer either to the file referred to by new or old before the operation began. Write access permission is required for both the directory containing old and the directory containing new.
But this function does not behave like this.
This is intended, or it is an issue need to fix? Or we just do not care it?

if (this.stringComparer(oldpath,newpath)!== 0 && this._getLinks(existingNode).size > 0) throw createIOError("ENOTEMPTY");
ShuiRuTian marked this conversation as resolved.
Show resolved Hide resolved
}
else {
if (isDirectory(existingNode)) throw createIOError("EISDIR");
Expand Down
2 changes: 1 addition & 1 deletion src/services/getEditsForFileRename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace ts {
const allFiles = program.getSourceFiles();
for (const sourceFile of allFiles) {
const newFromOld = oldToNew(sourceFile.path) as Path;
const newImportFromPath = newFromOld !== undefined ? newFromOld : sourceFile.path;
const newImportFromPath = newFromOld ?? sourceFile.path;
const newImportFromDirectory = getDirectoryPath(newImportFromPath);

const oldFromNew: string | undefined = newToOld(sourceFile.fileName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

// @Filename: /a/b/file1.ts
////import { foo } from "foo";

// @Filename: /a/node_modules/foo/index.d.ts
////export const foo = 0;

verify.getEditsForFileRename({
oldPath: "/a/b",
newPath: "/a/B",
newFileContents: {},
});