Skip to content

Commit

Permalink
Merge pull request #1980 from paulvanbrenk/tsbuildfix
Browse files Browse the repository at this point in the history
Fix issue when tsconfig.json has no outfile specified
  • Loading branch information
paulvanbrenk authored May 30, 2018
2 parents 017dd05 + f611307 commit 1a3e6ea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
31 changes: 17 additions & 14 deletions Nodejs/Product/Nodejs/Workspace/TsConfigScannerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ protected override async Task<List<FileReferenceInfo>> ComputeFileReferencesAsyn

var tsconfig = await TsConfigJsonFactory.CreateAsync(filePath);

Debug.Assert(!string.IsNullOrEmpty(tsconfig?.OutFile), "Should have an outfile specified.");
if (string.IsNullOrEmpty(tsconfig.OutFile))
{
return new List<FileReferenceInfo>();
}

var tsconfigFolder = Path.GetDirectoryName(filePath);
var outFile = Path.Combine(tsconfigFolder, tsconfig.OutFile);
Expand All @@ -64,7 +67,15 @@ protected override async Task<List<FileDataValue>> ComputeFileDataValuesAsync(st

var tsconfig = await TsConfigJsonFactory.CreateAsync(filePath);

Debug.Assert(!string.IsNullOrEmpty(tsconfig?.OutFile), "Should have an outfile specified.");
// Only use the tsconfig.json determine the debug target when there is an outfile specified,
// otherwise each .ts file can (in theory) be the entry point.
if (string.IsNullOrEmpty(tsconfig.OutFile))
{
return new List<FileDataValue> {
new FileDataValue(BuildConfigurationContext.ContextTypeGuid, filePath, null,
context: "Debug", target: null)
};
}

var tsconfigFolder = Path.GetDirectoryName(filePath);
var outFile = Path.Combine(tsconfigFolder, tsconfig.OutFile);
Expand All @@ -80,8 +91,8 @@ protected override async Task<List<FileDataValue>> ComputeFileDataValuesAsync(st
new FileDataValue(
DebugLaunchActionContext.ContextTypeGuid,
DebugLaunchActionContext.IsDefaultStartupProjectEntry,
launchSettings,
target: outFile),
launchSettings,
target: outFile),

new FileDataValue(BuildConfigurationContext.ContextTypeGuid, outFile, null,
context: "Debug", target: outFile),
Expand All @@ -93,17 +104,9 @@ protected override async Task<List<FileDataValue>> ComputeFileDataValuesAsync(st
return fileDataValues;
}

protected override async Task<bool> IsValidFileAsync(string filePath)
protected override Task<bool> IsValidFileAsync(string filePath)
{
// Only use the tsconfig.json determine the debug target when there is an outfile specified,
// otherwise each .ts file can (in theory) be the entry point.
if (TypeScriptHelpers.IsTsJsConfigJsonFile(filePath))
{
var tsconfig = await TsConfigJsonFactory.CreateAsync(filePath);
return !string.IsNullOrEmpty(tsconfig?.OutFile);
}

return false;
return Task.FromResult(TypeScriptHelpers.IsTsJsConfigJsonFile(filePath));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public override async Task<IFileContextActionResult> ExecuteAsync(IProgress<IFil
}
}


private sealed class BuildTsConfigContextAction : BuildFileContextAction, IFileContextAction, IVsCommandItem
{
public BuildTsConfigContextAction(string filePath, FileContext fileContext, OutputPaneWrapper outputPane)
Expand Down
33 changes: 19 additions & 14 deletions Nodejs/Product/Nodejs/Workspace/TypeScriptContextProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.NodejsTools.TypeScript;
Expand Down Expand Up @@ -47,19 +46,12 @@ public Task<IReadOnlyCollection<FileContext>> GetContextsForFileAsync(string fil

public async Task<IReadOnlyCollection<FileContext>> GetContextsForFileAsync(string filePath, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(filePath) || !IsSupportedFile(filePath))
if (string.IsNullOrEmpty(filePath) || !(await IsSupportedFileAsync(filePath)))
{
return FileContext.EmptyFileContexts;
}

var tsconfigJson = await this.workspace.IsContainedByTsConfig(filePath);

if (tsconfigJson != null)
{
return FileContext.EmptyFileContexts;
}

var list = new List<FileContext>
return new List<FileContext>
{
new FileContext(
ProviderTypeGuid,
Expand All @@ -68,13 +60,26 @@ public async Task<IReadOnlyCollection<FileContext>> GetContextsForFileAsync(stri
new[]{ filePath },
displayName: "TypeScript Build")
};

return list;
}

private static bool IsSupportedFile(string filePath)
private async Task<bool> IsSupportedFileAsync(string filePath)
{
return TypeScriptHelpers.IsTypeScriptFile(filePath) || TypeScriptHelpers.IsTsJsConfigJsonFile(filePath);
// config files are always good
if (TypeScriptHelpers.IsTsJsConfigJsonFile(filePath))
{
return true;
}

// TypeScript files should only build when there is no containing config file
if (TypeScriptHelpers.IsTypeScriptFile(filePath))
{
if (await this.workspace.IsContainedByTsConfig(filePath) == null)
{
return true;
}
}

return false;
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions Nodejs/Product/Nodejs/Workspace/TypeScriptScannerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.NodejsTools.Workspace
{
[ExportFileScanner(
ProviderType, "TypeScriptFile",
new string[] { "*.ts" },
new string[] { "*.ts", "*.tsx" },
new Type[] { typeof(IReadOnlyCollection<FileDataValue>), typeof(IReadOnlyCollection<FileReferenceInfo>) },
ProviderPriority.Normal)]
public sealed class TypeScriptScannerFactory : IWorkspaceProviderFactory<IFileScanner>
Expand Down Expand Up @@ -113,13 +113,23 @@ private async Task<string> DetermineOutFileAsync(string filePath)
var rootDir = Path.GetDirectoryName(tsConfig.FilePath);
var relativeTsFile = filePath.Substring(rootDir.Length).TrimStart('\\'); // save since we already know they have the same root

return this.workspace.MakeRelative(Path.Combine(rootDir, tsConfig.OutDir, Path.ChangeExtension(relativeTsFile, "js"))); // this works if outdir is null
return this.workspace.MakeRelative(Path.Combine(rootDir, tsConfig.OutDir, ChangeExtension(relativeTsFile))); // this works if outdir is null
}
else
{
return this.workspace.MakeRelative(Path.ChangeExtension(filePath, "js"));
return this.workspace.MakeRelative(ChangeExtension(filePath));
}
}

private static string ChangeExtension(string filePath)
{
if(filePath.EndsWith("tsx", StringComparison.OrdinalIgnoreCase))
{
return Path.ChangeExtension(filePath, "jsx");
}

return Path.ChangeExtension(filePath, "js");
}
}
}
}

0 comments on commit 1a3e6ea

Please sign in to comment.