dsherret / dsherret/ts-morph

ts-morph mutates the defaultCompilerOptions object passed to Project constructor

Open
#1,627 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

This is a subtle one, but bit me and took me a while to debug because I have a shared `defaultCompilerOptions` object that is used to instantiate multiple ts-morph `Project` instances.

Because this object is mutated when a `Project` is created, it ends up taking along the `lib` and `configFilePath` settings on to the next `Project` instance that is created. I was noticing that my second instance was "inheriting" the tsconfig settings from the first instance when the second instance doesn't configure a `tsconfig.json` file of its own.

Version: 25.0.1

**To Reproduce**

```ts
import { Project, ts } from "ts-morph";

const defaultCompilerOptions = {
jsx: ts.JsxEmit.React,
};

new Project({
defaultCompilerOptions,
});

console.log(defaultCompilerOptions);
// {
// jsx: 4,
// lib: ['lib.dom.d.ts', 'lib.es2020.d.ts'],
// configFilePath: 'path/to/tsconfig.json'
// }
```

**Expected behavior**

Object is not mutated.

I can work around this of course by always making a copy of the `defaultCompilerOptions` object, but figured I'd mention and save someone else from a little debugging time :)

Best,
Greg

Contributor guide

Open the contributing guide

Research direction

Start at the Project constructor handling of defaultCompilerOptions and trace where lib and configFilePath are added. Add a regression test using the reproduction's shared object, then verify the original object remains unchanged after creating a Project.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.