Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cryptic Error when ReferenceExpression value looks like a JSON object #7030

Open
1 task done
afscrome opened this issue Jan 6, 2025 · 1 comment
Open
1 task done
Labels
untriaged New issue has not been triaged

Comments

@afscrome
Copy link
Contributor

afscrome commented Jan 6, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If you try to create a reference expression encapsulating another expression in a json value (e.g. ReferenceExpression.Create($@"{{""url"": ""{app.GetEndpoint("http")}""}}), your resoruce will fail to start with a cryptic error of Failed to apply configuration value 'JsonifiedReference'. A dependency may have failed to start.

Further investigation suggested the root cause is an escaping problem, however aspire only logs the error at debug level, so you probably don't see the inner exception making determining the root cause a pain.

resourceLogger.LogCritical("Failed to apply configuration value '{ConfigKey}'. A dependency may have failed to start.", c.Key);

resourceLogger.LogCritical("Failed to apply configuration value '{ConfigKey}'. A dependency may have failed to start.", kvp.Key);

resourceLogger.LogCritical("Failed to apply arguments. A dependency may have failed to start.");

resourceLogger.LogCritical("Failed to apply container arguments '{ConfigKey}'. A dependency may have failed to start.", arg);

Expected Behavior

I'd have expected aspire to provide a clearer error that would have let me work out the issue was due to escaping, not a

Steps To Reproduce

For a more real world repo

var app = builder.AddExecutable("app", "pwsh", ".")
    .WithHttpEndpoint();
app.WithEnvironment("ENV VAR NAME", ReferenceExpression.Create($@"{{""url"": ""{app.GetEndpoint("http")}""}}"));

A simpler repo can be seen with

var expression = ReferenceExpression.Create($"{{");
var x = expression.ValueExpression; // Throws

Exceptions (if any)

Failed to apply configuration value 'JsonifiedReference'. A dependency may have failed to start.
Image

If you turn on debug logging, you'll see an error more like:

System.FormatException: Input string was not in a correct format. Failure to parse near offset 1. Format item ends prematurely.
at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ReadOnlySpan1 args) at System.String.FormatHelper(IFormatProvider provider, String format, ReadOnlySpan1 args)
at System.String.Format(IFormatProvider provider, String format, Object[] args)
at Aspire.Hosting.ApplicationModel.ReferenceExpression.get_ValueExpression() in /_/src/Aspire.Hosting/ApplicationModel/ReferenceExpression.cs:line 49
at Program.

$(String[] args) in S:\AspireApp2\AspireApp2.AppHost\Program.cs:line 18

.NET Version info

.NET SDK:
 Version:           9.0.100
 Commit:            59db016f11
 Workload version:  9.0.100-manifests.c6f19616
 MSBuild version:   17.12.7+5b8665660

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.100\

.NET workloads installed:
 [aspire]
   Installation Source: VS 17.12.35514.174
   Manifest Version:    8.2.2/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json
   Install Type:              Msi

Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.0
  Architecture: x64
  Commit:       9d5a6a9aa4

.NET SDKs installed:
  6.0.428 [C:\Program Files\dotnet\sdk]
  8.0.404 [C:\Program Files\dotnet\sdk]
  9.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Anything else?

@afscrome
Copy link
Contributor Author

afscrome commented Jan 6, 2025

I've opened a PR to exposing the underlying exception in #7032, although leaving this issue open to see if there's anything to improve the error message in this particular case. Whilst Input string was not in a correct format. Failure to parse near offset 1. Format item ends prematurely. is better than nothing, it's not immediately obvious it's related to escaping of curley braces.

@joperezr joperezr added the untriaged New issue has not been triaged label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
untriaged New issue has not been triaged
Projects
None yet
Development

No branches or pull requests

2 participants