Description
Tracks engineering work to enable strictNullChecks
in the VS Code code base.
Problem
A steady number of issues are reported each month that are caused by null reference exceptions. Most of these are just simple programming errors, such as forgetting to initialize a value or not realizing that a function may return undefined
.
Proposal
To address this class of errors, I propose that we move to enable strictNullChecks
in the main VS Code codebase. We have already successfully enabled strictNullChecks
for our build scripts and for our built-in extensions, and I believe that these changes have been highly beneficial in catching common errors and enforcing better programming practices.
Today, building VS Code with strictNullChecks
produces thousands of errors. Addressing these in a single pass is both impractical and would risk major regressions. I therefore propose we turn on strictNullChecks
in our code base incrementally, opting in individual files until eventually strictNullChecks
is enabled for the entire codebase.
Implementation
To accomplish this, a new tsconfig called tsconfig.strictNullChecks.json
will be introduced. This tsconfig will only be used for error checking. It will use include
and files
to compile only files that should be strict null checked.
We will build tsconfig.strictNullChecks.json
as part of our normal build process. A strictNullChecks
failure in tsconfig.strictNullChecks.json
should cause the entire VS Code build to fail. This should prevent us from regressing strictNullChecks
changes that have already been added
How to help
This will be an long term engineer effort. The good news is that I believe we can do this incrementally and that each incremental change will improve the codebase in its own right.
Here's what's needed to make this work:
For regular development
Make sure your changes do not break the tsconfig.strictNullChecks.json
build. You can check this manually by running: yarn strict-null-check
This script s part of our build pipelines. It is not run as part of yarn compile
or yarn watch
for performance reasons (doing so would essentially have us building the VS Code codebase twice)
Add a new file to be strict null checked
-
Add the file in the
files
section ofsrc/tsconfig.strictNullChecks.json
"files": [ "./vs/base/common/iterator.ts" ]
Keep in mind that this will also strict null check all files that the added file imports. Therefore it's best to start with files that either have very few imports or that only import files that are strict null checked.
-
Run
yarn strict-null-check
oryarn strict-null-check -- --watch
-
Fix all null check related errors. In general, this should involve:
- Annotate nullable types and fix type errors
- Use the
!
not null operator to tell TypeScript that a value cannot be null. Only use this if you are certain that a value absolutely cannot benull
orundefined
(i.e. you already checked this in code) - Add conditionals to handle null. Only change the actual logic if you believe that strict null checks has revealed a real programming error that must be handled.
-
Make sure you have not broken the normal VS Code build.
-
Check in your changes