Skip to content

Commit ec7cfcb

Browse files
committed
Fix file size estimation when bundling symlinks
FileInfo.Length will return the size of a symlink, rather than the file itself. On a source-built dotnet SDK, a trivial test project will bundle files like: packs/Microsoft.NETCore.App.Runtime.linux-x64/10.0.0-preview.7.25380.108/runtimes/linux-x64/lib/net10.0/Microsoft.CSharp.dll Which is a symlink to: ../../../../../../../shared/Microsoft.NETCore.App/10.0.0-preview.7.25380.108/Microsoft.CSharp.dll This can result in: > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: The "GenerateBundle" task failed unexpectedly. [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: System.NotSupportedException: Unable to expand length of this stream beyond its capacity. [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at System.IO.UnmanagedMemoryStream.WriteCore(ReadOnlySpan`1 buffer) [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize) [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at Microsoft.NET.HostModel.Bundle.Bundler.AddToBundle(Stream bundle, FileStream file, FileType type) [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at Microsoft.NET.HostModel.Bundle.Bundler.GenerateBundle(IReadOnlyList`1 fileSpecs) [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateBundle.ExecuteCore() [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Execute() [/build/test/test.fsproj] > [...]/Microsoft.NET.Publish.targets(1132,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(TaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMo Fixes: b79c4fb
1 parent 3c6b264 commit ec7cfcb

File tree

5 files changed

+11
-3
lines changed

5 files changed

+11
-3
lines changed

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<SystemThreadingAccessControlVersion>7.0.0</SystemThreadingAccessControlVersion>
9898
<!-- Keep toolset versions in sync with dotnet/msbuild and dotnet/sdk -->
9999
<MicrosoftBclAsyncInterfacesToolsetVersion>8.0.0</MicrosoftBclAsyncInterfacesToolsetVersion>
100+
<MicrosoftIoRedistToolsetVersion>6.1.3</MicrosoftIoRedistToolsetVersion>
100101
<SystemBuffersToolsetVersion>4.5.1</SystemBuffersToolsetVersion>
101102
<SystemCollectionsImmutableToolsetVersion>8.0.0</SystemCollectionsImmutableToolsetVersion>
102103
<SystemMemoryToolsetVersion>4.5.5</SystemMemoryToolsetVersion>

src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void RewriteAppHost(MemoryMappedFile mappedFile, MemoryMappedViewAccessor access
124124
if (File.Exists(appHostDestinationFilePath))
125125
File.Delete(appHostDestinationFilePath);
126126

127-
long appHostSourceLength = new FileInfo(appHostSourceFilePath).Length;
127+
long appHostSourceLength = HostModelUtils.GetFileLength(appHostSourceFilePath);
128128
string destinationFileName = Path.GetFileName(appHostDestinationFilePath);
129129
// Memory-mapped files cannot be resized, so calculate
130130
// the maximum length of the destination file upfront.

src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public string GenerateBundle(IReadOnlyList<FileSpec> fileSpecs)
293293
// We will memory map a larger file than needed, but we'll take that trade-off.
294294
foreach (var (spec, type) in relativePathToSpec)
295295
{
296-
bundledFilesSize += new FileInfo(spec.SourcePath).Length;
296+
bundledFilesSize += HostModelUtils.GetFileLength(spec.SourcePath);
297297
if (type == FileType.Assembly)
298298
{
299299
// Alignment could be as much as AssemblyAlignment - 1 bytes.
@@ -314,7 +314,7 @@ public string GenerateBundle(IReadOnlyList<FileSpec> fileSpecs)
314314
{
315315
Directory.CreateDirectory(destinationDirectory);
316316
}
317-
var hostLength = new FileInfo(hostSource).Length;
317+
var hostLength = HostModelUtils.GetFileLength(hostSource);
318318
var bundleManifestLength = Manifest.GetManifestLength(BundleManifest.BundleMajorVersion, relativePathToSpec.Select(x => x.Spec.BundleRelativePath));
319319
long bundleTotalSize = hostLength + bundledFilesSize + bundleManifestLength;
320320
if (_target.IsOSX && _macosCodesign)

src/installer/managed/Microsoft.NET.HostModel/HostModelUtils.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@ public static (int ExitCode, string StdErr) RunCodesign(string args, string appH
3232
return (p.ExitCode, p.StandardError.ReadToEnd());
3333
}
3434
}
35+
36+
public static long GetFileLength(string path)
37+
{
38+
var info = new FileInfo(path);
39+
return ((FileInfo?)info.ResolveLinkTarget(true) ?? info).Length;
40+
}
3541
}
3642
}

src/installer/managed/Microsoft.NET.HostModel/Microsoft.NET.HostModel.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageDownloadAndReference Include="System.Memory" Version="$(SystemMemoryToolsetVersion)" Folder="lib/net461" />
2525
<PackageDownloadAndReference Include="System.Text.Json" Version="$(SystemTextJsonToolsetVersion)" Folder="lib/net462" />
2626
<PackageDownloadAndReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataToolsetVersion)" Folder="lib/net462" />
27+
<PackageDownloadAndReference Include="System.IO.Redist" Version="$(MicrosoftIoRedistVersion)" Folder="lib/net462" />
2728
</ItemGroup>
2829

2930
<ItemGroup>

0 commit comments

Comments
 (0)