microsoft / microsoft/aspire

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

Open
#10,970 0 comments 0 reactions 0 assignees View on GitHub
area-app-model
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

### Is there an existing issue for this?

- [x] 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
```cs
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:

```json
[
"-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.

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.