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 @@ -28,7 +28,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
6 changes: 3 additions & 3 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{}, host, nil)
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestCheckSrcCompiler(t *testing.T) {

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{}, host, nil)
assert.Equal(t, len(errors), 0, "Expected no errors in parsed command line")
p := compiler.NewProgram(compiler.ProgramOptions{
Expand All @@ -87,7 +87,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{}, 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/modulespecifiers"
"github.com/microsoft/typescript-go/internal/outputpaths"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/transformers/declarations"
"github.com/microsoft/typescript-go/internal/tsoptions"
Expand All @@ -24,6 +25,7 @@ type EmitHost interface {
GetCurrentDirectory() string
CommonSourceDirectory() string
IsEmitBlocked(file string) bool
PnpApi() *pnp.PnpApi
}

var _ EmitHost = (*emitHost)(nil)
Expand Down Expand Up @@ -107,6 +109,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 @@ -4,6 +4,7 @@ import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
"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 @@ -17,6 +18,7 @@ type CompilerHost interface {
Trace(msg string)
GetSourceFile(opts ast.SourceFileParseOptions) *ast.SourceFile
GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine
PnpApi() *pnp.PnpApi
}

var _ CompilerHost = (*compilerHost)(nil)
Expand All @@ -27,34 +29,39 @@ type compilerHost struct {
defaultLibraryPath string
extendedConfigCache tsoptions.ExtendedConfigCache
trace func(msg string)
pnpApi *pnp.PnpApi
}

func NewCachedFSCompilerHost(
currentDirectory string,
fs vfs.FS,
defaultLibraryPath string,
extendedConfigCache tsoptions.ExtendedConfigCache,
pnpApi *pnp.PnpApi,
trace func(msg string),
) 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 string),
) CompilerHost {
if trace == nil {
trace = func(msg string) {}
}

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

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

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

func (h *compilerHost) Trace(msg string) {
h.trace(msg)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/microsoft/typescript-go/internal/modulespecifiers"
"github.com/microsoft/typescript-go/internal/outputpaths"
"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 @@ -78,6 +79,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, 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/tspath"
"github.com/microsoft/typescript-go/internal/vfs"
"github.com/microsoft/typescript-go/internal/vfs/cachedvfs"
Expand Down Expand Up @@ -42,6 +43,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
14 changes: 13 additions & 1 deletion internal/core/compileroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/pnp"
"github.com/microsoft/typescript-go/internal/tspath"
)

Expand Down Expand Up @@ -300,7 +301,7 @@ func (options *CompilerOptions) GetStrictOptionValue(value Tristate) bool {
return options.Strict == TSTrue
}

func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (result []string, fromConfig bool) {
func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string, pnpApi *pnp.PnpApi) (result []string, fromConfig bool) {
if options.TypeRoots != nil {
return options.TypeRoots, true
}
Expand All @@ -316,6 +317,17 @@ func (options *CompilerOptions) GetEffectiveTypeRoots(currentDirectory string) (
}
}

nmTypes, nmFromConfig := options.GetNodeModulesTypeRoots(baseDir)

if pnpApi != nil {
typeRoots, fromConfig := pnpApi.AppendPnpTypeRoots(nmTypes, baseDir, nmFromConfig)
return typeRoots, fromConfig
}

return nmTypes, nmFromConfig
}

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
5 changes: 5 additions & 0 deletions internal/execute/build/compilerHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package build
import (
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/compiler"
"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 @@ -27,6 +28,10 @@ func (h *compilerHost) GetCurrentDirectory() string {
return h.host.GetCurrentDirectory()
}

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

func (h *compilerHost) Trace(msg string) {
h.trace(msg)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/execute/build/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/microsoft/typescript-go/internal/compiler"
"github.com/microsoft/typescript-go/internal/execute/incremental"
"github.com/microsoft/typescript-go/internal/execute/tsc"
"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 Down Expand Up @@ -45,6 +46,10 @@ func (h *host) GetCurrentDirectory() string {
return h.host.GetCurrentDirectory()
}

func (h *host) PnpApi() *pnp.PnpApi {
return h.host.PnpApi()
}

func (h *host) Trace(msg string) {
panic("build.Orchestrator.host does not support tracing, use a different host for tracing")
}
Expand Down
1 change: 1 addition & 0 deletions internal/execute/build/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ func NewOrchestrator(opts Options) *Orchestrator {
orchestrator.opts.Sys.FS(),
orchestrator.opts.Sys.DefaultLibraryPath(),
nil,
orchestrator.opts.Sys.PnpApi(),
nil,
),
mTimes: &collections.SyncMap[tspath.Path, time.Time]{},
Expand Down
4 changes: 2 additions & 2 deletions internal/execute/tsc.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func performIncrementalCompilation(
compileTimes *tsc.CompileTimes,
testing tsc.CommandLineTesting,
) tsc.CommandLineResult {
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys, testing))
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, sys.PnpApi(), getTraceFromSys(sys, testing))
buildInfoReadStart := sys.Now()
oldProgram := incremental.ReadBuildInfoProgram(config, incremental.NewBuildInfoReader(host), host)
compileTimes.BuildInfoReadTime = sys.Now().Sub(buildInfoReadStart)
Expand Down Expand Up @@ -288,7 +288,7 @@ func performCompilation(
compileTimes *tsc.CompileTimes,
testing tsc.CommandLineTesting,
) tsc.CommandLineResult {
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, getTraceFromSys(sys, testing))
host := compiler.NewCachedFSCompilerHost(sys.GetCurrentDirectory(), sys.FS(), sys.DefaultLibraryPath(), extendedConfigCache, sys.PnpApi(), getTraceFromSys(sys, testing))
// todo: cache, statistics, tracing
parseStart := sys.Now()
program := compiler.NewProgram(compiler.ProgramOptions{
Expand Down
Loading
Loading