Skip to content

fix: don't mutate the defaultSettings object passed to SettingsContainer#1675

Open
Yanhu007 wants to merge 1 commit intodsherret:latestfrom
Yanhu007:fix/settings-container-mutates-input
Open

fix: don't mutate the defaultSettings object passed to SettingsContainer#1675
Yanhu007 wants to merge 1 commit intodsherret:latestfrom
Yanhu007:fix/settings-container-mutates-input

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #1627

Problem

SettingsContainer stores a direct reference to the defaultSettings object:

constructor(defaultSettings: T) {
    this.#defaultSettings = Object.assign({}, defaultSettings); // ✅ copy
    this._settings = defaultSettings;                           // ❌ direct reference
}

When set() is later called, Object.assign(this._settings, settings) mutates the original object. This means a shared defaultCompilerOptions object gets extra properties (lib, configFilePath) after creating a Project, and those leak into subsequent Project instances.

Fix

Create a shallow copy for _settings, matching the existing behavior of #defaultSettings:

this._settings = Object.assign({}, defaultSettings);

One-character fix (Object.assign({}, ...) instead of direct assignment).

SettingsContainer stores a direct reference to the passed-in
defaultSettings object as _settings. When set() is later called,
Object.assign(this._settings, settings) mutates the original
object, causing unexpected side effects for callers who reuse
the same options object across multiple Project instances.

Create a shallow copy for _settings, matching the existing
behavior of #defaultSettings.

Fixes dsherret#1627
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.

ts-morph mutates the defaultCompilerOptions object passed to Project constructor

1 participant