Skip to content

Commit 669ed52

Browse files
committed
Improving temp file handling in F# compilation service
1 parent 2b3899d commit 669ed52

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/WebJobs.Script/Description/DotNet/FSharp/FSharpCompilationService.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,16 @@ public ICompilation GetFunctionCompilation(FunctionMetadata functionMetadata)
8383

8484
FSharpErrorInfo[] errors = null;
8585
FSharpOption<Assembly> assemblyOption = null;
86-
string scriptFilePath = Path.Combine(Path.GetTempPath(), Path.GetFileName(functionMetadata.ScriptFile));
8786

88-
var asmName = FunctionAssemblyLoader.GetAssemblyNameFromMetadata(functionMetadata, compilation.AssemblyName);
89-
var dllName = Path.GetTempPath() + asmName + ".dll";
90-
var pdbName = Path.ChangeExtension(dllName, "pdb");
87+
string scriptPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
88+
89+
Directory.CreateDirectory(scriptPath);
90+
91+
string scriptFilePath = Path.Combine(scriptPath, Path.GetFileName(functionMetadata.ScriptFile));
92+
93+
var assemblyName = FunctionAssemblyLoader.GetAssemblyNameFromMetadata(functionMetadata, compilation.AssemblyName);
94+
var assemblyFileName = Path.Combine(scriptPath, assemblyName + ".dll");
95+
var pdbName = Path.ChangeExtension(assemblyFileName, "pdb");
9196

9297
try
9398
{
@@ -112,7 +117,6 @@ public ICompilation GetFunctionCompilation(FunctionMetadata functionMetadata)
112117

113118
var otherFlags = new List<string>();
114119

115-
// For some reason CompileToDynamicAssembly wants "fsc.exe" as the first arg, it is ignored.
116120
otherFlags.Add("fsc.exe");
117121

118122
// The --noframework option is used because we will shortly add references to mscorlib and FSharp.Core
@@ -159,7 +163,7 @@ public ICompilation GetFunctionCompilation(FunctionMetadata functionMetadata)
159163
otherFlags.Add("--lib:" + Path.Combine(Path.GetDirectoryName(functionMetadata.ScriptFile), DotNetConstants.PrivateAssembliesFolderName));
160164
}
161165

162-
otherFlags.Add("--out:" + dllName);
166+
otherFlags.Add("--out:" + assemblyFileName);
163167

164168
// Get the #load closure
165169
FSharpChecker checker = FSharpChecker.Create(null, null, null, msbuildEnabled: FSharpOption<bool>.Some(false));
@@ -183,7 +187,7 @@ public ICompilation GetFunctionCompilation(FunctionMetadata functionMetadata)
183187

184188
if (code == 0)
185189
{
186-
var assemblyBytes = File.ReadAllBytes(dllName);
190+
var assemblyBytes = File.ReadAllBytes(assemblyFileName);
187191
byte[] pdbBytes = null;
188192
if (File.Exists(pdbName))
189193
{
@@ -195,7 +199,7 @@ public ICompilation GetFunctionCompilation(FunctionMetadata functionMetadata)
195199
}
196200
finally
197201
{
198-
Task.WhenAll(DeleteIfExistsAsync(scriptFilePath), DeleteIfExistsAsync(dllName), DeleteIfExistsAsync(pdbName))
202+
DeleteDirectoryAsync(scriptPath, recursive: true)
199203
.ContinueWith(t => t.Exception.Handle(e =>
200204
{
201205
// TODO: Trace

src/WebJobs.Script/Extensions/FileUtility.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ namespace Microsoft.Azure.WebJobs.Script
1212
{
1313
public static class FileUtility
1414
{
15+
public static Task DeleteDirectoryAsync(string path, bool recursive)
16+
{
17+
return Task.Run(() =>
18+
{
19+
if (Directory.Exists(path))
20+
{
21+
Directory.Delete(path, recursive);
22+
}
23+
});
24+
}
25+
1526
public static Task DeleteIfExistsAsync(string path)
1627
{
1728
return Task.Run(() =>

0 commit comments

Comments
 (0)