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(57451): Prevent self-imports when using the "Move to File" refactor #57530

Merged
merged 1 commit into from
Mar 20, 2024

Conversation

eloyrobillard
Copy link
Contributor

Fixes #57451

@eloyrobillard
Copy link
Contributor Author

@microsoft-github-policy-service agree

Comment on lines 432 to 440
const pathToTargetFileWithExtension = resolvePath(getDirectoryPath(oldFile.path), targetFileName);

// no self-imports
if (sourceFile.fileName === pathToTargetFileWithExtension) return;

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const pathToTargetFileWithExtension = resolvePath(getDirectoryPath(oldFile.path), targetFileName);
// no self-imports
if (sourceFile.fileName === pathToTargetFileWithExtension) return;
const pathToTargetFileWithExtension = resolvePath(getDirectoryPath(getNormalizedAbsolutePath(oldFile.fileName, program.getCurrentDirectory())), targetFileName);
// no self imports
if (getStringComparer(!program.useCaseSensitiveFileNames())(pathToTargetFileWithExtension, sourceFile.fileName) === Comparison.EqualTo) return;

Copy link
Member

Choose a reason for hiding this comment

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

@navya9singh we also need to replace usage of sourceFile.path in makeImportOrReqiure so its not using canonical path but actual file name:

 const pathToTargetFile = resolvePath(getDirectoryPath(sourceFile.path), targetFileNameWithExtension);

to

const pathToTargetFile = resolvePath(getDirectoryPath(getNormalizedAbsolutePath(sourceFile.fileName, program.getCurrentDirectory())), targetFileNameWithExtension);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made the changes

@navya9singh
Copy link
Member

Thanks for working on this! This pr also fixes #54309

@sheetalkamat sheetalkamat merged commit ede8ad8 into microsoft:main Mar 20, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect import destination after using Move to file
4 participants