Skip to content

Ensure we don't copy OrderedMaps #1003

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

Merged
merged 1 commit into from
May 31, 2025
Merged

Ensure we don't copy OrderedMaps #1003

merged 1 commit into from
May 31, 2025

Conversation

jakebailey
Copy link
Member

@jakebailey jakebailey commented May 30, 2025

Fixes #978

#978 broke because removeWildcardFilesWithLowerPriorityExtension passed the map by value and then attempted to delete from it. That will cause the internal map to be modified, but the key slice to not be modified in the caller. The fix is to pass by pointer.

To prevent this bug from happening in the future, use the noCopy pattern to make go vet check it. This reveals another place we weren't passing the map by pointer. That one's not a bug, but it could have been.

I don't have a real test for the problem as I think it's self explanatory and go vet will catch this, but I could figure out how to test it if required.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses a bug where modifying an OrderedMap copy did not update its key slice in the caller, and introduces the noCopy pattern to prevent accidental copies. Key changes include:

  • Changing the parameter of removeWildcardFilesWithLowerPriorityExtension to a pointer.
  • Updating OrderedMap's MarshalJSON receiver from a value to a pointer.
  • Adjusting hasCommonDeclaration to use pointer semantics for collections.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
internal/tsoptions/tsconfigparsing.go Updated function signature and call to pass OrderedMap by pointer
internal/collections/ordered_map.go Embedded noCopy and modified MarshalJSON receiver to prevent accidental copy
internal/checker/checker.go Changed parameter type in hasCommonDeclaration to pointer for consistency
Comments suppressed due to low confidence (3)

internal/tsoptions/tsconfigparsing.go:1474

  • Changing the parameter to a pointer fixes the bug where the map's key slice was not updated in the caller. Please verify that all callers now pass a pointer to ensure correct behavior.
func removeWildcardFilesWithLowerPriorityExtension(file string, wildcardFiles *collections.OrderedMap[string, string], extensions [][]string, keyMapper func(value string) string) {

internal/collections/ordered_map.go:206

  • Switching to a pointer receiver for MarshalJSON avoids unintended copying of OrderedMap, ensuring that the noCopy pattern is effective. This change improves maintainability and safety.
func (m *OrderedMap[K, V]) MarshalJSON() ([]byte, error) {

internal/checker/checker.go:20197

  • Updating the parameter to a pointer ensures that the collection is passed correctly without inadvertent copies. Confirm that related functions and callers are updated accordingly.
func (c *Checker) hasCommonDeclaration(symbols *collections.OrderedSet[*ast.Symbol]) bool {

@jakebailey jakebailey added this pull request to the merge queue May 31, 2025
keys []K
mp map[K]V
}

// noCopy may be embedded into structs which must not be copied
Copy link
Member

Choose a reason for hiding this comment

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

May be we should do this for orderedSet and syncmap as well ?

Merged via the queue into main with commit e7a30f5 May 31, 2025
23 checks passed
@jakebailey jakebailey deleted the jabaile/fix-978 branch May 31, 2025 00:43
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.

lsp hangs when a javascript and typescript file have the same name
2 participants