C
C#2y ago
ascpixi

✅ (solved!) "System.InvalidOperationException: Sequence contains no elements" error from Roslyn

Hi guys! I'm trying to publish my C# project using dotnet publish -c Debug --nologo -r win-x64, but I'm getting a cryptic error message, which I've attached to this post. (<MSBuild> refers to C:\Program Files\dotnet\sdk\<version>\Roslyn\Microsoft.CSharp.Core.targets) I have no idea how to fix this, to be honest - I've looked this up on Google but to no avail. I'm using shared projects, so maybe that's causing some issues...?
28 Replies
ascpixi
ascpixiOP2y ago
Here's the full log:
Thinker
Thinker2y ago
Looks like a Roslyn bug?
ascpixi
ascpixiOP2y ago
that's what i hope is not the case... i'm looking at the -v diag output right now, but that doesn't really give me any clues on what's happening if that is a roslyn bug, a repro case would be extremely difficult to create because i don't even know what causes it xd
Thinker
Thinker2y ago
You can try asking in #roslyn since someone there might have more insight regarding the stack trace
ascpixi
ascpixiOP2y ago
this is what it spits out right before crashing
ascpixi
ascpixiOP2y ago
alright, i'll send a link to the thread there
youssef13
youssef132y ago
This should be a Roslyn bug. You need to open an issue at dotnet/roslyn repository, and it would be great if you can upload a repro project to the issue
ascpixi
ascpixiOP2y ago
i identified what line caused the error after removing this specific line, the error disappears (more appear, but they're unrelated)
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);
but that's such a random line for it to fail on... alright i have no idea why removing that one line does anything, i tried putting what crashes in my solution into a new one and it compiles fine
sanic
sanic2y ago
what's the type of value1? Are you able to share the project which the compiler is crashing on?
ascpixi
ascpixiOP2y ago
i'm working on a repro project right now, it really does seem that line causes the issue since i'm able to get it to get to the linking stage (which ofc is dependent on roslyn working) value1 is T (it's a generic method) alright oddly enough now when i uncomment it now it works correctly??
sanic
sanic2y ago
version of the compiler in use? (can determine by adding #error version to your code)
ascpixi
ascpixiOP2y ago
Compiler version: '4.4.0-6.22608.27 (af1e46ad)
ascpixi
ascpixiOP2y ago
oh yep i fixed link errors and im getting that error again
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System
{
public static class HashCode
{
const uint EmptyState = 1;

public static int Combine<T1>(T1 value1)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);

uint hash = EmptyState;
hash += 4;

hash = QueueRound(hash, hc1);

hash = MixFinal(hash);
return (int)hash;
}

private static uint MixFinal(uint hash)
{
return 0;
}

private static uint QueueRound(uint hash, uint queuedValue)
{
return 0;
}

private static uint Round(uint hash, uint input)
{
return 0;
}
}
}
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System
{
public static class HashCode
{
const uint EmptyState = 1;

public static int Combine<T1>(T1 value1)
{
uint hc1 = (uint)(value1?.GetHashCode() ?? 0);

uint hash = EmptyState;
hash += 4;

hash = QueueRound(hash, hc1);

hash = MixFinal(hash);
return (int)hash;
}

private static uint MixFinal(uint hash)
{
return 0;
}

private static uint QueueRound(uint hash, uint queuedValue)
{
return 0;
}

private static uint Round(uint hash, uint input)
{
return 0;
}
}
}
maybe it's something to do with shared projects...? they are very buggy in my experience i did
<Compile Include="$(MSBuildThisFileDirectory)**\*.cs" />
<Compile Include="$(MSBuildThisFileDirectory)**\*.cs" />
and VS does not play well with that but if i don't do that, then it wants to add an entry to the projitems for every file deleted/added, which is very prone to merge conflicts
sanic
sanic2y ago
I think this is saying: the type of the a?.b expression is a Nullable, so we essentially want to make an expression like new int?(a.b), and for some reason, we found no constructor on int? with a single parameter. Is there any chance your project is referencing a bogus definition of System.Nullable?
ascpixi
ascpixiOP2y ago
oh, yeah! i don't have a single param constructor there
using System;
using System.Collections.Generic;

namespace System
{
/// <summary>
/// Represents a value type that can be assigned <see langword="null"/>.
/// </summary>
/// <typeparam name="T">The underlying value type of the <see cref="Nullable{T}"/> generic type.</typeparam>
public struct Nullable<T> where T : struct
{
private readonly bool _hasValue;
private T _value;

/// <summary>
/// Gets a value indicating whether the current <see cref="Nullable{T}"/> object has a valid value of its underlying type.
/// </summary>
public bool HasValue => _hasValue;

/// <summary>
/// Gets the value of the current <see cref="Nullable{T}"/> object if it has been assigned a valid underlying value.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the <see cref="HasValue"/> property is <see langword="false"/>.</exception>
public T Value {
get {
if(!_hasValue) {
throw new InvalidOperationException("Nullable object must have a value.");
}

return _value;
}
}
}
}
using System;
using System.Collections.Generic;

namespace System
{
/// <summary>
/// Represents a value type that can be assigned <see langword="null"/>.
/// </summary>
/// <typeparam name="T">The underlying value type of the <see cref="Nullable{T}"/> generic type.</typeparam>
public struct Nullable<T> where T : struct
{
private readonly bool _hasValue;
private T _value;

/// <summary>
/// Gets a value indicating whether the current <see cref="Nullable{T}"/> object has a valid value of its underlying type.
/// </summary>
public bool HasValue => _hasValue;

/// <summary>
/// Gets the value of the current <see cref="Nullable{T}"/> object if it has been assigned a valid underlying value.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the <see cref="HasValue"/> property is <see langword="false"/>.</exception>
public T Value {
get {
if(!_hasValue) {
throw new InvalidOperationException("Nullable object must have a value.");
}

return _value;
}
}
}
}
lemme implement one real quick
Thinker
Thinker2y ago
Is this a polyfill or something?
ascpixi
ascpixiOP2y ago
yyyyep, seems to be it... nope
sanic
sanic2y ago
D:
ascpixi
ascpixiOP2y ago
i'm implementing my own runtime osdev with C#, very novel concept
sanic
sanic2y ago
Well! I'm glad I could help figure it out..
Thinker
Thinker2y ago
wow, that's cool
ascpixi
ascpixiOP2y ago
yeah, thanks for the help!!
sanic
sanic2y ago
It might be good if you could file a bug on us If compiler needs to use a well-known member like this, we usually want it to report a binding-time error if the member doesn't exist Not sure if bug will be prioritized 😉 but good to have a record of it.
ascpixi
ascpixiOP2y ago
what repo is best to file it under? dotnet/roslyn?
ascpixi
ascpixiOP2y ago
https://github.com/dotnet/roslyn/issues/67709 issue filed! (hope the maintainers aren't gonna laugh at me since this is such a specific issue lol)
GitHub
Missing Nullable(T) constructor triggers an LINQ empty sequence exc...
Version Used: 4.4.0-6.22608.27 (af1e46a) Steps to Reproduce: Create an MSBuild NativeAOT C# project, and set <NoStdLib> to true. Create a basic runtime implementation. The ZeroSharp no-runtim...
sanic
sanic2y ago
thanks! And don't worry about it. 🙂 These are bugs we would probably file on ourselves if we discovered them, even if we might not go out of our way to fix them.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server