Skip to content

Analyzer to detect ReferenceExpressions ending up as regular interpolated strings rather than ReferenceExpressions #10970

@afscrome

Description

@afscrome

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I tried to use the following to run a sql script against the database

builder.AddExecutable("dbMigrator", "sqlcmd", packageDir)
    .WithArgs(
        "-S", $"127.0.0.1,{sql.Resource.PrimaryEndpoint.Property(EndpointProperty.Port)}",
        "-U", "sa",
        "-P", sql.Resource.PasswordParameter,
        "-i", patchedUpgradeScriptPath
    )

However because WithArgs() takes an object array, the -S argument was treated as a regular interpolated string and not a reference expression, with the final args ending up as:

[
  "-S",
  // Expected `Aspire.Hosting.ApplicationModel.EndpointReferenceExpression`
  "127.0.0.1,Aspire.Hosting.ApplicationModel.EndpointReferenceExpression",
  "-U",
  "sa",
  "-P",
  "REDACTED",
  "-i",
  "REDACTED"
]

Describe the solution you'd like

I'd like to see some kind of early detection to help prevent such errors. Two ideas:

  • Make ToString() on ReferenceExpressions throw / log an error, rather than ending up
  • An analyzer to detect when a Reference Expression is detected within a regular interpolated string.

Additional context

For anyone hitting the original issue, you can work around that by explicitly building a ReferenceExpression, and then referencing that.

var migrations = builder.AddExecutable("dbMigrator", "sqlcmd", packageDir)
    .WithArgs(
        "-S", serverExpresison,
        "-U", "sa",
        "-P", sql.Resource.PasswordParameter,
        "-i", patchedUpgradeScriptPath
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-app-modelIssues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions