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
9 changes: 8 additions & 1 deletion _extension/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ export class Client {
this.clientOptions = {
documentSelector: [
...jsTsLanguageModes.map(language => ({ scheme: "file", language })),
...jsTsLanguageModes.map(language => ({ scheme: "untitled", language })),
...jsTsLanguageModes.map(language => ({
scheme: "untitled",
language,
})),
...jsTsLanguageModes.map(language => ({
scheme: "zip",
language,
})),
],
outputChannel: this.outputChannel,
traceOutputChannel: this.traceOutputChannel,
Expand Down
17 changes: 16 additions & 1 deletion cmd/tsgo/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (

"github.com/microsoft/typescript-go/internal/bundled"
"github.com/microsoft/typescript-go/internal/execute/tsc"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
"github.com/microsoft/typescript-go/internal/vfs/pnpvfs"
"golang.org/x/term"
)

Expand All @@ -20,6 +22,7 @@ type osSys struct {
defaultLibraryPath string
cwd string
start time.Time
pnpApi *pnp.PnpApi
}

func (s *osSys) SinceStart() time.Duration {
Expand Down Expand Up @@ -59,18 +62,30 @@ func (s *osSys) GetEnvironmentVariable(name string) string {
return os.Getenv(name)
}

func (s *osSys) PnpApi() *pnp.PnpApi {
return s.pnpApi
}

func newSystem() *osSys {
cwd, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting current directory: %v\n", err)
os.Exit(int(tsc.ExitStatusInvalidProject_OutputsSkipped))
}

var fs vfs.FS = osvfs.FS()

pnpApi := pnp.InitPnpApi(fs, tspath.NormalizePath(cwd))
if pnpApi != nil {
fs = pnpvfs.From(fs)
}

return &osSys{
cwd: tspath.NormalizePath(cwd),
fs: bundled.WrapFS(osvfs.FS()),
fs: bundled.WrapFS(fs),
defaultLibraryPath: bundled.LibPath(),
writer: os.Stdout,
start: time.Now(),
pnpApi: pnpApi,
}
}
11 changes: 10 additions & 1 deletion internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
"github.com/microsoft/typescript-go/internal/bundled"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/project"
"github.com/microsoft/typescript-go/internal/project/logging"
"github.com/microsoft/typescript-go/internal/vfs"
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
"github.com/microsoft/typescript-go/internal/vfs/pnpvfs"
)

//go:generate go tool golang.org/x/tools/cmd/stringer -type=MessageType -output=stringer_generated.go
Expand Down Expand Up @@ -93,12 +95,19 @@ func NewServer(options *ServerOptions) *Server {
panic("Cwd is required")
}

var fs vfs.FS = osvfs.FS()

pnpApi := pnp.InitPnpApi(fs, options.Cwd)
if pnpApi != nil {
fs = pnpvfs.From(fs)
}

server := &Server{
r: bufio.NewReader(options.In),
w: bufio.NewWriter(options.Out),
stderr: options.Err,
cwd: options.Cwd,
fs: bundled.WrapFS(osvfs.FS()),
fs: bundled.WrapFS(fs),
defaultLibraryPath: options.DefaultLibraryPath,
}
logger := logging.NewLogger(options.Err)
Expand Down
4 changes: 2 additions & 2 deletions internal/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ foo.bar;`
fs = bundled.WrapFS(fs)

cd := "/"
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, nil)
host := compiler.NewCompilerHost(cd, fs, bundled.LibPath(), nil, nil, nil)

parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile("/tsconfig.json", &core.CompilerOptions{}, nil, host, nil)
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
Expand Down Expand Up @@ -68,7 +68,7 @@ func BenchmarkNewChecker(b *testing.B) {

rootPath := tspath.CombinePaths(tspath.NormalizeSlashes(repo.TypeScriptSubmodulePath), "src", "compiler")

host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil, nil)
parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), &core.CompilerOptions{}, nil, host, nil)
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
p := compiler.NewProgram(compiler.ProgramOptions{
Expand Down
3 changes: 3 additions & 0 deletions internal/compiler/emitHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/microsoft/typescript-go/internal/module"
"github.com/microsoft/typescript-go/internal/outputpaths"
"github.com/microsoft/typescript-go/internal/packagejson"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/symlinks"
"github.com/microsoft/typescript-go/internal/transformers/declarations"
Expand All @@ -25,6 +26,7 @@ type EmitHost interface {
GetCurrentDirectory() string
CommonSourceDirectory() string
IsEmitBlocked(file string) bool
PnpApi() *pnp.PnpApi
}

var _ EmitHost = (*emitHost)(nil)
Expand Down Expand Up @@ -108,6 +110,7 @@ func (host *emitHost) Options() *core.CompilerOptions { return host.program.Opti
func (host *emitHost) SourceFiles() []*ast.SourceFile { return host.program.SourceFiles() }
func (host *emitHost) GetCurrentDirectory() string { return host.program.GetCurrentDirectory() }
func (host *emitHost) CommonSourceDirectory() string { return host.program.CommonSourceDirectory() }
func (host *emitHost) PnpApi() *pnp.PnpApi { return host.program.PnpApi() }
func (host *emitHost) UseCaseSensitiveFileNames() bool {
return host.program.UseCaseSensitiveFileNames()
}
Expand Down
13 changes: 12 additions & 1 deletion internal/compiler/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/diagnostics"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/tsoptions"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
Expand All @@ -18,6 +19,7 @@ type CompilerHost interface {
Trace(msg *diagnostics.Message, args ...any)
GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine
PnpApi() *pnp.PnpApi
}

var _ CompilerHost = (*compilerHost)(nil)
Expand All @@ -27,6 +29,7 @@ type compilerHost struct {
fs vfs.FS
defaultLibraryPath string
extendedConfigCache tsoptions.ExtendedConfigCache
pnpApi *pnp.PnpApi
trace func(msg *diagnostics.Message, args ...any)
}

Expand All @@ -35,27 +38,31 @@ func NewCachedFSCompilerHost(
fs vfs.FS,
defaultLibraryPath string,
extendedConfigCache tsoptions.ExtendedConfigCache,
pnpApi *pnp.PnpApi,
trace func(msg *diagnostics.Message, args ...any),
) CompilerHost {
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache, trace)
return NewCompilerHost(currentDirectory, cachedvfs.From(fs), defaultLibraryPath, extendedConfigCache, pnpApi, trace)
}

func NewCompilerHost(
currentDirectory string,
fs vfs.FS,
defaultLibraryPath string,
extendedConfigCache tsoptions.ExtendedConfigCache,
pnpApi *pnp.PnpApi,
trace func(msg *diagnostics.Message, args ...any),
) CompilerHost {
if trace == nil {
trace = func(msg *diagnostics.Message, args ...any) {}
}

return &compilerHost{
currentDirectory: currentDirectory,
fs: fs,
defaultLibraryPath: defaultLibraryPath,
extendedConfigCache: extendedConfigCache,
trace: trace,
pnpApi: pnpApi,
}
}

Expand All @@ -71,6 +78,10 @@ func (h *compilerHost) GetCurrentDirectory() string {
return h.currentDirectory
}

func (h *compilerHost) PnpApi() *pnp.PnpApi {
return h.pnpApi
}

func (h *compilerHost) Trace(msg *diagnostics.Message, args ...any) {
h.trace(msg, args...)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/microsoft/typescript-go/internal/outputpaths"
"github.com/microsoft/typescript-go/internal/packagejson"
"github.com/microsoft/typescript-go/internal/parser"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/sourcemap"
Expand Down Expand Up @@ -87,6 +88,11 @@ func (p *Program) GetCurrentDirectory() string {
return p.Host().GetCurrentDirectory()
}

// PnpApi implements checker.Program.
func (p *Program) PnpApi() *pnp.PnpApi {
return p.Host().PnpApi()
}

// GetGlobalTypingsCacheLocation implements checker.Program.
func (p *Program) GetGlobalTypingsCacheLocation() string {
return "" // !!! see src/tsserver/nodeServer.ts for strada's node-specific implementation
Expand Down
6 changes: 3 additions & 3 deletions internal/compiler/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestProgram(t *testing.T) {
CompilerOptions: &opts,
},
},
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil),
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil, nil),
})

actualFiles := []string{}
Expand Down Expand Up @@ -280,7 +280,7 @@ func BenchmarkNewProgram(b *testing.B) {
CompilerOptions: &opts,
},
},
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil),
Host: compiler.NewCompilerHost("c:/dev/src", fs, bundled.LibPath(), nil, nil, nil),
}

for b.Loop() {
Expand All @@ -297,7 +297,7 @@ func BenchmarkNewProgram(b *testing.B) {
fs := osvfs.FS()
fs = bundled.WrapFS(fs)

host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil)
host := compiler.NewCompilerHost(rootPath, fs, bundled.LibPath(), nil, nil, nil)

parsed, errors := tsoptions.GetParsedCommandLineOfConfigFile(tspath.CombinePaths(rootPath, "tsconfig.json"), nil, nil, host, nil)
assert.Equal(b, len(errors), 0, "Expected no errors in parsed command line")
Expand Down
6 changes: 6 additions & 0 deletions internal/compiler/projectreferencedtsfakinghost.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/module"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/symlinks"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
Expand Down Expand Up @@ -43,6 +44,11 @@ func (h *projectReferenceDtsFakingHost) GetCurrentDirectory() string {
return h.host.GetCurrentDirectory()
}

// PnpApi implements module.ResolutionHost.
func (h *projectReferenceDtsFakingHost) PnpApi() *pnp.PnpApi {
return h.host.PnpApi()
}

type projectReferenceDtsFakingVfs struct {
projectReferenceFileMapper *projectReferenceFileMapper
dtsDirectories collections.Set[tspath.Path]
Expand Down
10 changes: 10 additions & 0 deletions internal/core/compileroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (
if options.TypeRoots != nil {
return options.TypeRoots, true
}
baseDir := options.GetBaseDirFromOptions(currentDirectory)

nmTypes, nmFromConfig := options.GetNodeModulesTypeRoots(baseDir)
return nmTypes, nmFromConfig
}

func (options *CompilerOptions) GetBaseDirFromOptions(currentDirectory string) string {
var baseDir string
if options.ConfigFilePath != "" {
baseDir = tspath.GetDirectoryPath(options.ConfigFilePath)
Expand All @@ -316,7 +323,10 @@ func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (
panic("cannot get effective type roots without a config file path or current directory")
}
}
return baseDir
}

func (options *CompilerOptions) GetNodeModulesTypeRoots(baseDir string) (result []string, fromConfig bool) {
typeRoots := make([]string, 0, strings.Count(baseDir, "/"))
tspath.ForEachAncestorDirectory(baseDir, func(dir string) (any, bool) {
typeRoots = append(typeRoots, tspath.CombinePaths(dir, "node_modules", "@types"))
Expand Down
32 changes: 32 additions & 0 deletions internal/diagnostics/diagnostics_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading