Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tsc/internal/api/session_createprogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,49 @@ func TestCreateProgramReportsNonCompositeProjectReference(t *testing.T) {
}
assert.DeepEqual(t, codes, []int32{6306})
}

func TestCreateSnapshotResolvesPathsRelativeToParsedConfigDirectory(t *testing.T) {
t.Parallel()

const (
mainFile = "/project/main.ts"
libFile = "/project/types/lib.d.ts"
)
init, _ := projecttestutil.GetSessionInitOptions(map[string]any{
mainFile: `import { x } from "lib"; export const y: number = x;`,
libFile: `export declare const x: number;`,
}, nil, &projecttestutil.TypingsInstallerOptions{})
init.Options.CurrentDirectory = "/somewhere-else"
session := NewStandaloneSession(init, nil)
defer session.Close()

var parseParams ParseJsonConfigFileContentParams
assert.NilError(t, json.Unmarshal([]byte(`{
"json": {
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"noLib": true,
"types": [],
"paths": { "lib": ["./types/lib.d.ts"] }
}
},
"configDirectory": "/project"
}`), &parseParams))
parsed, err := session.handleParseJsonConfigFileContent(context.Background(), &parseParams)
assert.NilError(t, err)

// Round-trip the parsed options through JSON as an API client would.
encodedOptions, err := json.Marshal(parsed.Options)
assert.NilError(t, err)
var createParams CreateSnapshotParams
assert.NilError(t, json.Unmarshal([]byte(`{"createPrograms":[{"rootFiles":["`+mainFile+`"],"compilerOptions":`+string(encodedOptions)+`}]}`), &createParams))

response, err := session.handleCreateSnapshot(context.Background(), &createParams)
assert.NilError(t, err)
snapshot, err := session.getSnapshotData(response.Snapshot)
assert.NilError(t, err)
program, err := snapshot.getProgram(response.Projects[0].Id)
assert.NilError(t, err)
assert.Assert(t, program.GetSourceFile(libFile) != nil, "expected 'lib' to resolve via paths relative to /project")
}
2 changes: 2 additions & 0 deletions tsc/internal/tsoptions/rawcompileroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func (o *RawCompilerOptions) Finalize(basePath tspath.RootedDirectoryPath) (*cor
}
} else if key == "configFilePath" {
pathKind = CommandLineOptionPathKindFile
} else if key == "pathsBasePath" {
pathKind = CommandLineOptionPathKindDirectory
} else if key != "allowNonTsExtensions" && key != "suppressOutputPathCheck" {
continue
}
Expand Down
2 changes: 2 additions & 0 deletions tsc/internal/tsoptions/rawcompileroptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestRawCompilerOptionsFinalizePaths(t *testing.T) {
"allowNonTsExtensions": true,
"suppressOutputPathCheck": true,
"configFilePath": "",
"pathsBasePath": "config",
"NoImplicitAny": true
}`), &raw))

Expand All @@ -46,5 +47,6 @@ func TestRawCompilerOptionsFinalizePaths(t *testing.T) {
assert.Equal(t, options.AllowNonTsExtensions, core.TSTrue)
assert.Equal(t, options.SuppressOutputPathCheck, core.TSTrue)
assert.Equal(t, options.ConfigFilePath, tspath.RootedFilePath(""))
assert.Equal(t, options.PathsBasePath, tspath.RootedDirectoryPath("/project/config"))
assert.Equal(t, options.NoImplicitAny, core.TSUnknown)
}
Loading