-
Notifications
You must be signed in to change notification settings - Fork 891
Fixes a crash when renaming import path of a JSON file #3196
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
Closed
Andarist
wants to merge
1
commit into
microsoft:main
from
Andarist:fix/rename-json-import-path-crash
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
internal/fourslash/tests/importAllowRenameOfImportPath2_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/core" | ||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/ls/lsutil" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestAllowRenameOfImportPath2(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `// @Filename: /src/example.ts | ||
| import brushPackageJson from './visx-brush/[|package|].json'; | ||
| // @Filename: /src/visx-brush/package.json | ||
| { "name": "brush" } | ||
| ` | ||
|
|
||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| prefs := &lsutil.UserPreferences{ | ||
| AllowRenameOfImportPath: core.TSTrue, | ||
| } | ||
| f.VerifyBaselineRename(t, prefs, f.Ranges()[0]) | ||
| } |
27 changes: 27 additions & 0 deletions
27
internal/fourslash/tests/importAllowRenameOfImportPath3_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/core" | ||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/ls/lsutil" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestAllowRenameOfImportPath3(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `// @Filename: /src/example.ts | ||
| import stuff from './[|stuff|].cts'; | ||
| // @Filename: /src/stuff.cts | ||
| export = { name: "stuff" }; | ||
| ` | ||
|
|
||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| prefs := &lsutil.UserPreferences{ | ||
| AllowRenameOfImportPath: core.TSTrue, | ||
| } | ||
| f.VerifyBaselineRename(t, prefs, f.Ranges()[0]) | ||
| } |
29 changes: 29 additions & 0 deletions
29
internal/fourslash/tests/importAllowRenameOfImportPath4_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/core" | ||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/ls/lsutil" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestAllowRenameOfImportPath4(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `// @Filename: /src/example.ts | ||
| import styles from './[|styles|].css'; | ||
| // @Filename: /src/styles.css | ||
| body { color: red; } | ||
| // @Filename: /src/globals.d.ts | ||
| declare module "*.css"; | ||
| ` | ||
|
|
||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| prefs := &lsutil.UserPreferences{ | ||
| AllowRenameOfImportPath: core.TSTrue, | ||
| } | ||
| f.VerifyBaselineRename(t, prefs, f.Ranges()[0]) | ||
| } |
27 changes: 27 additions & 0 deletions
27
internal/fourslash/tests/importAllowRenameOfImportPath5_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package fourslash_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/microsoft/typescript-go/internal/core" | ||
| "github.com/microsoft/typescript-go/internal/fourslash" | ||
| "github.com/microsoft/typescript-go/internal/ls/lsutil" | ||
| "github.com/microsoft/typescript-go/internal/testutil" | ||
| ) | ||
|
|
||
| func TestAllowRenameOfImportPath5(t *testing.T) { | ||
| t.Parallel() | ||
| defer testutil.RecoverAndFail(t, "Panic on fourslash test") | ||
| const content = `// @Filename: /src/example.ts | ||
| import styles from './[|styles|].css'; | ||
| // @Filename: /src/styles.css | ||
| body { color: red; } | ||
| ` | ||
|
|
||
| f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content) | ||
| defer done() | ||
| prefs := &lsutil.UserPreferences{ | ||
| AllowRenameOfImportPath: core.TSTrue, | ||
| } | ||
| f.VerifyBaselineRename(t, prefs, f.Ranges()[0]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...baselines/reference/fourslash/findRenameLocations/allowRenameOfImportPath2.baseline.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // === findRenameLocations === | ||
| // === /src/example.ts === | ||
| // import brushPackageJson from '[|./visx-brush//*RENAME*/package.jsonRENAME|]'; |
7 changes: 7 additions & 0 deletions
7
...baselines/reference/fourslash/findRenameLocations/allowRenameOfImportPath3.baseline.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // === findRenameLocations === | ||
| // === /src/example.ts === | ||
| // import stuff from '[|.//*RENAME*/stuff.ctsRENAME|]'; | ||
|
|
||
| // === /src/stuff.cts === | ||
| // [|exportRENAME|] = { name: "stuff" }; | ||
| // |
3 changes: 3 additions & 0 deletions
3
...baselines/reference/fourslash/findRenameLocations/allowRenameOfImportPath4.baseline.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // === findRenameLocations === | ||
| // === /src/example.ts === | ||
| // import styles from './/*RENAME*/styles.css'; |
3 changes: 3 additions & 0 deletions
3
...baselines/reference/fourslash/findRenameLocations/allowRenameOfImportPath5.baseline.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // === findRenameLocations === | ||
| // === /src/example.ts === | ||
| // import styles from './/*RENAME*/styles.css'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tested the same patch in Strada and renaming the
from "./x/package.json"resulted in the file itself being renamed too. That's definitely a desirable outcome but I'm not exactly sure what's the codepath leading to that filename rename - given it works... I wasn't exactly inclined to dig deeper.The alternative fix would be to handle this in the loop here:
typescript-go/internal/ls/rename.go
Lines 82 to 92 in a4325da
The crash happens in
getTextForRenameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to think a bit more about all this, but I think this fix is ok. We're missing support for file rename in Corsa right now (Strada's
getEditsForFileRename), planning on porting that next. But I think the right behavior would be to return just aRenameFilein this case and then compute the edits in response to awillRenameFilesrequest from the client. So the fix looks right, but we don't have a good way right now to properly test this. I think I will leave this PR open and revisit once file rename is ported, if that's ok.