Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPa
{
symbolsUpload.RemoveUploadFromGradleFile();

if (_options is { Il2CppLineNumberSupportEnabled: true })
if (_options is { Enabled: true, Il2CppLineNumberSupportEnabled: true })
Copy link
Contributor Author

@bitsandfoxes bitsandfoxes Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated but: To reduce logging noise when the SDK is disabled.

{
_logger.LogWarning("The IL2CPP line number support requires the debug symbol upload to be enabled.");
}
Expand All @@ -327,6 +327,9 @@ internal void SetupSymbolsUpload(string unityProjectPath, string gradleProjectPa
SentryCli.CreateSentryProperties(launcherDirectory, _sentryCliOptions!, _options!);

symbolsUpload.TryCopySymbolsToGradleProject();
// We need to remove the old upload task first as the project persists across consecutive builds and the
// cli options might have changed
symbolsUpload.RemoveUploadFromGradleFile();
symbolsUpload.AppendUploadToGradleFile(sentryCliPath);
}
catch (Exception e)
Expand Down
27 changes: 17 additions & 10 deletions src/Sentry.Unity.Editor/Android/DebugSymbolUpload.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -30,8 +31,8 @@ internal class DebugSymbolUpload
private readonly List<string> _symbolUploadPaths;
private readonly string _mappingFilePath;

private const string SymbolUploadTaskStartComment = "// Autogenerated Sentry symbol upload task [start]";
private const string SymbolUploadTaskEndComment = "// Autogenerated Sentry symbol upload task [end]";
internal const string SymbolUploadTaskStartComment = "// Autogenerated Sentry symbol upload task [start]";
internal const string SymbolUploadTaskEndComment = "// Autogenerated Sentry symbol upload task [end]";
private const string SentryCliMarker = "SENTRY_CLI";
private const string UploadArgsMarker = "UPLOAD_ARGS";
private const string ProguardArgsMarker = "PROGUARD_ARGS";
Expand Down Expand Up @@ -102,8 +103,6 @@ public DebugSymbolUpload(IDiagnosticLogger logger,

public void AppendUploadToGradleFile(string sentryCliPath)
{
RemoveUploadFromGradleFile();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now happens outside and before calling this method. Added a sanity check before actually writing to disk.


_logger.LogInfo("Appending debug symbols upload task to gradle file.");

sentryCliPath = ConvertSlashes(sentryCliPath);
Expand Down Expand Up @@ -162,10 +161,17 @@ public void AppendUploadToGradleFile(string sentryCliPath)
symbolUploadText = symbolUploadText.Replace(UploadArgsMarker, uploadDifArguments);
symbolUploadText = symbolUploadText.Replace(ProguardArgsMarker, uploadProguardArguments);

using var streamWriter = File.AppendText(_gradleScriptPath);
streamWriter.WriteLine(SymbolUploadTaskStartComment);
streamWriter.WriteLine(symbolUploadText);
streamWriter.WriteLine(SymbolUploadTaskEndComment);
var gradleBuildFile = LoadGradleScript();
if (gradleBuildFile.Contains("task sentryUploadSymbols"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Inconsistent Task Detection Causes Build Failures

The AppendUploadToGradleFile method checks for an existing Sentry upload task using a simple Contains("task sentryUploadSymbols"). This differs from RemoveUploadFromGradleFile's marker-based detection and is prone to false positives if the string appears in comments. This inconsistency can lead to unnecessary build failures when appending the task, or prevent proper cleanup of existing tasks.

Fix in Cursor Fix in Web

{
throw new InvalidOperationException("Failed to create Debug Symbol Upload Task. Task already exists in gradle file. Consider creating a clean build.");
}

var stringBuilder = new StringBuilder(gradleBuildFile);
stringBuilder.AppendLine(SymbolUploadTaskStartComment);
stringBuilder.AppendLine(symbolUploadText);
stringBuilder.AppendLine(SymbolUploadTaskEndComment);
File.WriteAllText(_gradleScriptPath, stringBuilder.ToString());
}

private string LoadGradleScript()
Expand All @@ -182,13 +188,14 @@ public void RemoveUploadFromGradleFile()
{
_logger.LogDebug("Attempting to remove the previous upload task from the gradle project.");
var gradleBuildFile = LoadGradleScript();
if (!gradleBuildFile.Contains("sentry.properties"))
if (!gradleBuildFile.Contains(SymbolUploadTaskStartComment))
{
_logger.LogDebug("No previous upload task found.");
return;
}

var regex = new Regex(Regex.Escape(SymbolUploadTaskStartComment) + ".*" + Regex.Escape(SymbolUploadTaskEndComment), RegexOptions.Singleline);
var pattern = Regex.Escape(SymbolUploadTaskStartComment) + @"\r?\n.*?\r?\n" + Regex.Escape(SymbolUploadTaskEndComment);
var regex = new Regex(pattern, RegexOptions.Singleline);
gradleBuildFile = regex.Replace(gradleBuildFile, "");

using var streamWriter = File.CreateText(_gradleScriptPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public void AppendUploadToGradleFile_BuildGradleFileDoesNotExist_ThrowsFileNotFo
Assert.AreEqual(GetGradleFilePath(), ex.FileName);
}

[Test]
public void AppendUploadToGradleFile_TaskAlreadyExists_ThrowsInvalidOperationException()
{
var sut = _fixture.GetSut();
sut.AppendUploadToGradleFile(_fixture.SentryCliPath);

var ex = Assert.Throws<InvalidOperationException>(() => sut.AppendUploadToGradleFile(_fixture.SentryCliPath));
StringAssert.Contains("Task already exists in gradle file", ex.Message);
}

[Test]
[TestCase(false, false, false)]
[TestCase(true, false, false)]
Expand Down Expand Up @@ -214,6 +224,29 @@ public void RemoveUploadTaskFromGradleFile_UploadHasBeenAdded_RemovesUploadTask(
StringAssert.DoesNotContain("sentry.properties", actualFileContent);
}

[Test]
public void RemoveUploadTaskFromGradleFile_MultipleConsecutiveRemovals_HandlesCorrectly()
{
var sut = _fixture.GetSut();

// Add and remove multiple times to simulate consecutive builds
for (var i = 0; i < 3; i++)
{
var gradleFile = File.ReadAllText(GetGradleFilePath());
StringAssert.DoesNotContain("task sentryUploadSymbols", gradleFile);

sut.AppendUploadToGradleFile(_fixture.SentryCliPath);

gradleFile = File.ReadAllText(GetGradleFilePath());
StringAssert.Contains("task sentryUploadSymbols", gradleFile);

sut.RemoveUploadFromGradleFile();

gradleFile = File.ReadAllText(GetGradleFilePath());
StringAssert.DoesNotContain("task sentryUploadSymbols", gradleFile);
}
}

[Test]
[TestCase("2019.4")]
[TestCase("2020.3")]
Expand Down
Loading