Skip to content

Fixes a crash when renaming import path of a JSON file#3196

Open
Andarist wants to merge 1 commit intomicrosoft:mainfrom
Andarist:fix/rename-json-import-path-crash
Open

Fixes a crash when renaming import path of a JSON file#3196
Andarist wants to merge 1 commit intomicrosoft:mainfrom
Andarist:fix/rename-json-import-path-crash

Conversation

@Andarist
Copy link
Contributor

fixes a crash found here #3187 (comment) (it also crashes in Strada)

Comment on lines +1383 to +1385
if options.use == referenceUseRename && ast.IsSourceFile(decl) {
continue
}
Copy link
Contributor Author

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:

for _, entry := range entries {
uri := l.getFileNameOfEntry(entry)
if l.UserPreferences().AllowRenameOfImportPath != core.TSTrue && entry.node != nil && ast.IsStringLiteralLike(entry.node) && ast.TryGetImportFromModuleSpecifier(entry.node) != nil {
continue
}
textEdit := &lsproto.TextEdit{
Range: l.getRangeOfEntry(entry),
NewText: l.getTextForRename(data.OriginalNode, entry, params.NewName, ch, quotePreference),
}
changes[uri] = append(changes[uri], textEdit)
}

The crash happens in getTextForRename

Copy link
Member

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 a RenameFile in this case and then compute the edits in response to a willRenameFiles request 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.

@gabritto gabritto self-assigned this Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants