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

Blank MAUI shows CS0103 "The name 'InitializeComponent' does not exist in the current context" #4098

Closed
Redth opened this issue Jan 12, 2022 · 3 comments · Fixed by #4235
Closed
Assignees
Labels
area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging area-xaml XAML, CSS, Triggers, Behaviors fixed-in-6.0.200-preview.13.2 Look for this fix in 6.0.200-preview.13.2! t/bug Something isn't working

Comments

@Redth
Copy link
Member

Redth commented Jan 12, 2022

Description

From: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1459138

When creating a new solution, often there are build errors referencing the XamlG (generated code behind for XAML).
Closing the solution and reopening usually makes the errors go away.

image

Related:
#3281

@Redth Redth added the t/bug Something isn't working label Jan 12, 2022
@Redth Redth added the area-xaml XAML, CSS, Triggers, Behaviors label Jan 12, 2022
@jonathanpeppers
Copy link
Member

So far the logs from MSBuild give us:

 15:43:19.857     1>Done Building Project "C:\Users\jopepper\source\repos\MauiApp40\MauiApp40\MauiApp40.csproj" (ResolveFrameworkReferencesDesignTime;ResolveProjectReferencesDesignTime2;CollectResolvedSDKReferencesDesignTime;CollectPackageReferences;ResolveComReferencesDesignTime;BuiltProjectOutputGroup;CollectFrameworkReferences;CollectUpToDateCheckBuiltDesignTime;CollectPackageDownloads;ResolveAssemblyReferencesDesignTime;GenerateSupportedTargetFrameworkAlias;CollectAnalyzersDesignTime;CollectUpToDateCheckInputDesignTime;CollectUpToDateCheckOutputDesignTime;ResolvePackageDependenciesDesignTime;CollectSuggestedWorkloads;CollectCentralPackageVersions;CompileDesignTime;CollectResolvedCompilationReferencesDesignTime target(s)) -- FAILED.

 
Project Performance Summary:
        1 ms  C:\Users\jopepper\source\repos\MauiApp40\MauiApp40\MauiApp40.csproj   1 calls
        1 ms  ResolveFrameworkReferencesDesignTime;ResolveProjectReferencesDesignTime2;CollectResolvedSDKReferencesDesignTime;CollectPackageReferences;ResolveComReferencesDesignTime;BuiltProjectOutputGroup;CollectFrameworkReferences;CollectUpToDateCheckBuiltDesignTime;CollectPackageDownloads;ResolveAssemblyReferencesDesignTime;GenerateSupportedTargetFrameworkAlias;CollectAnalyzersDesignTime;CollectUpToDateCheckInputDesignTime;CollectUpToDateCheckOutputDesignTime;ResolvePackageDependenciesDesignTime;CollectSuggestedWorkloads;CollectCentralPackageVersions;CompileDesignTime;CollectResolvedCompilationReferencesDesignTime   1 calls
 
Build FAILED.
    0 Warning(s)
    0 Error(s)

No other errors in the log.

Next steps, I'm getting further instructions on debugging MSBuild...

jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 13, 2022
The source generator was doing:

    using (var reader = File.OpenText(projItem.AdditionalText.Path))

The problem with this, is you don't actually have to hit Ctrl+S in
Visual Studio to save the files. This is potentially loading stale
files on disk.

Instead we can use:

    projItem.AdditionalText.GetText().ToString();

And this will give us the string contents of the file from the
in-memory Roslyn state.

This might solve some of the weird issues in the IDE such as:

dotnet#4098

However, I'm still able to reproduce dotnet#4098 after making this change.
Redth pushed a commit that referenced this issue Jan 13, 2022
The source generator was doing:

    using (var reader = File.OpenText(projItem.AdditionalText.Path))

The problem with this, is you don't actually have to hit Ctrl+S in
Visual Studio to save the files. This is potentially loading stale
files on disk.

Instead we can use:

    projItem.AdditionalText.GetText().ToString();

And this will give us the string contents of the file from the
in-memory Roslyn state.

This might solve some of the weird issues in the IDE such as:

#4098

However, I'm still able to reproduce #4098 after making this change.
@jonathanpeppers
Copy link
Member

We found unchecking this setting solves this problem:

image

But I also have the changes from #4128 locally.

The Roslyn team is investigating.

@Redth
Copy link
Member Author

Redth commented Jan 14, 2022

@hartez hartez added the area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging label Jan 14, 2022
@Redth Redth moved this from Todo to In Progress in [ARCHIVED] MAUI Planning Jan 19, 2022
jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 20, 2022
Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1459138
Fixes: dotnet#4098

This partially reverts 974cac4.

Creating a new MAUI solution, results in several intellisense errors:

    CS0103: The name 'InitializeComponent` does not exist in the current context.

The missing members are created by the (previously XamlG) Roslyn
source generator. You can go-to definition the missing members, build
& run works, and if you close/reopen the solution the errors go away.
Unchecking the `Run code analysis in a separate process`, also seems
to solve the issue.

.NET MAUI seems to be using an intersection of features that causes
this problem:

1. We have a `dotnet new maui` project template.
2. We have a source generator in a NuGet package.
3. We have MSBuild targets that setup the source generator in a NuGet
   package.
4. We use `AdditionalFiles` to pass `.xaml` files in to the source
   generator.

What happens on the first project load, the design-time builds
*before* project restore are missing the source generator. So the call
to `<Csc/>` does not have `@(Analyzer)` or `AdditionalFiles` at all.

To solve this issue, we can move everything *out* of NuGet packages,
so they become regular "SDK" or workload packs:

* `Microsoft.Maui.Controls.Build.Tasks` is no longer packable
    * all files moved to `Microsoft.Maui.Sdk`
* `Resizetizer.csproj` is no longer packable
    * actual files are in `Microsoft.Maui.Resizetizer.Sdk`
* `$(MauiVersion)` no longer applies to build tasks

Testing locally, this seems to solve the problem for me. I can see the
`@(Analyzer)` and `AdditionalFiles` being passed on all design-time
builds now.

Other changes:

* I added `%(IsImplicitlyDefined)` to the source generator assembly. I
  just noticed this was missing while reading `.binlog` files.
* I put all files in the `Sdk` folder in `Microsoft.Maui.Sdk`. This
  seems a bit simpler to have one directory.
jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 20, 2022
Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1459138
Fixes: dotnet#4098

This partially reverts 974cac4.

Creating a new MAUI solution, results in several intellisense errors:

    CS0103: The name 'InitializeComponent` does not exist in the current context.

The missing members are created by the (previously XamlG) Roslyn
source generator. You can go-to definition the missing members, build
& run works, and if you close/reopen the solution the errors go away.
Unchecking the `Run code analysis in a separate process`, also seems
to solve the issue.

.NET MAUI seems to be using an intersection of features that causes
this problem:

1. We have a `dotnet new maui` project template.
2. We have a source generator in a NuGet package.
3. We have MSBuild targets that setup the source generator in a NuGet
   package.
4. We use `AdditionalFiles` to pass `.xaml` files in to the source
   generator.

What happens on the first project load, the design-time builds
*before* project restore are missing the source generator. So the call
to `<Csc/>` does not have `@(Analyzer)` or `AdditionalFiles` at all.

To solve this issue, we can move everything *out* of NuGet packages,
so they become regular "SDK" or workload packs:

* `Microsoft.Maui.Controls.Build.Tasks` is no longer packable
    * all files moved to `Microsoft.Maui.Sdk`
* `Resizetizer.csproj` is no longer packable
    * actual files are in `Microsoft.Maui.Resizetizer.Sdk`
* `$(MauiVersion)` no longer applies to build tasks

Testing locally, this seems to solve the problem for me. I can see the
`@(Analyzer)` and `AdditionalFiles` being passed on all design-time
builds now.

Other changes:

* I added `%(IsImplicitlyDefined)` to the source generator assembly. I
  just noticed this was missing while reading `.binlog` files.
* I put all files in the `Sdk` folder in `Microsoft.Maui.Sdk`. This
  seems a bit simpler to have one directory.
jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 20, 2022
Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1459138
Fixes: dotnet#4098

This partially reverts 974cac4.

Creating a new MAUI solution, results in several intellisense errors:

    CS0103: The name 'InitializeComponent` does not exist in the current context.

The missing members are created by the (previously XamlG) Roslyn
source generator. You can go-to definition the missing members, build
& run works, and if you close/reopen the solution the errors go away.
Unchecking the `Run code analysis in a separate process`, also seems
to solve the issue.

.NET MAUI seems to be using an intersection of features that causes
this problem:

1. We have a `dotnet new maui` project template.
2. We have a source generator in a NuGet package.
3. We have MSBuild targets that setup the source generator in a NuGet
   package.
4. We use `AdditionalFiles` to pass `.xaml` files in to the source
   generator.

What happens on the first project load, the design-time builds
*before* project restore are missing the source generator. So the call
to `<Csc/>` does not have `@(Analyzer)` or `AdditionalFiles` at all.

To solve this issue, we can move everything *out* of NuGet packages,
so they become regular "SDK" or workload packs:

* `Microsoft.Maui.Controls.Build.Tasks` is no longer packable
    * all files moved to `Microsoft.Maui.Sdk`
* `Resizetizer.csproj` is no longer packable
    * actual files are in `Microsoft.Maui.Resizetizer.Sdk`
* `$(MauiVersion)` no longer applies to build tasks

Testing locally, this seems to solve the problem for me. I can see the
`@(Analyzer)` and `AdditionalFiles` being passed on all design-time
builds now.

Other changes:

* I added `%(IsImplicitlyDefined)` to the source generator assembly. I
  just noticed this was missing while reading `.binlog` files.
* I put all files in the `Sdk` folder in `Microsoft.Maui.Sdk`. This
  seems a bit simpler to have one directory.
jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 20, 2022
Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1459138
Fixes: dotnet#4098

This partially reverts 974cac4.

Creating a new MAUI solution, results in several intellisense errors:

    CS0103: The name 'InitializeComponent` does not exist in the current context.

The missing members are created by the (previously XamlG) Roslyn
source generator. You can go-to definition the missing members, build
& run works, and if you close/reopen the solution the errors go away.
Unchecking the `Run code analysis in a separate process`, also seems
to solve the issue.

.NET MAUI seems to be using an intersection of features that causes
this problem:

1. We have a `dotnet new maui` project template.
2. We have a source generator in a NuGet package.
3. We have MSBuild targets that setup the source generator in a NuGet
   package.
4. We use `AdditionalFiles` to pass `.xaml` files in to the source
   generator.

What happens on the first project load, the design-time builds
*before* project restore are missing the source generator. So the call
to `<Csc/>` does not have `@(Analyzer)` or `AdditionalFiles` at all.

To solve this issue, we can move everything *out* of NuGet packages,
so they become regular "SDK" or workload packs:

* `Microsoft.Maui.Controls.Build.Tasks` is no longer packable
    * all files moved to `Microsoft.Maui.Sdk`
* `Resizetizer.csproj` is no longer packable
    * actual files are in `Microsoft.Maui.Resizetizer.Sdk`
* `$(MauiVersion)` no longer applies to build tasks

Testing locally, this seems to solve the problem for me. I can see the
`@(Analyzer)` and `AdditionalFiles` being passed on all design-time
builds now.

Other changes:

* I added `%(IsImplicitlyDefined)` to the source generator assembly. I
  just noticed this was missing while reading `.binlog` files.
* I put all files in the `Sdk` folder in `Microsoft.Maui.Sdk`. This
  seems a bit simpler to have one directory.
jonathanpeppers added a commit to jonathanpeppers/maui that referenced this issue Jan 20, 2022
Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1459138
Fixes: dotnet#4098

This partially reverts 974cac4.

Creating a new MAUI solution, results in several intellisense errors:

    CS0103: The name 'InitializeComponent` does not exist in the current context.

The missing members are created by the (previously XamlG) Roslyn
source generator. You can go-to definition the missing members, build
& run works, and if you close/reopen the solution the errors go away.
Unchecking the `Run code analysis in a separate process`, also seems
to solve the issue.

.NET MAUI seems to be using an intersection of features that causes
this problem:

1. We have a `dotnet new maui` project template.
2. We have a source generator in a NuGet package.
3. We have MSBuild targets that setup the source generator in a NuGet
   package.
4. We use `AdditionalFiles` to pass `.xaml` files in to the source
   generator.

What happens on the first project load, the design-time builds
*before* project restore are missing the source generator. So the call
to `<Csc/>` does not have `@(Analyzer)` or `AdditionalFiles` at all.

To solve this issue, we can move everything *out* of NuGet packages,
so they become regular "SDK" or workload packs:

* `Microsoft.Maui.Controls.Build.Tasks` is no longer packable
    * all files moved to `Microsoft.Maui.Sdk`
* `Resizetizer.csproj` is no longer packable
    * actual files are in `Microsoft.Maui.Resizetizer.Sdk`
* `$(MauiVersion)` no longer applies to build tasks

Testing locally, this seems to solve the problem for me. I can see the
`@(Analyzer)` and `AdditionalFiles` being passed on all design-time
builds now.

Other changes:

* I added `%(IsImplicitlyDefined)` to the source generator assembly. I
  just noticed this was missing while reading `.binlog` files.
* I put all files in the `Sdk` folder in `Microsoft.Maui.Sdk`. This
  seems a bit simpler to have one directory.
Redth pushed a commit that referenced this issue Jan 21, 2022
Fixes: https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1459138
Fixes: #4098

This partially reverts 974cac4.

Creating a new MAUI solution, results in several intellisense errors:

    CS0103: The name 'InitializeComponent` does not exist in the current context.

The missing members are created by the (previously XamlG) Roslyn
source generator. You can go-to definition the missing members, build
& run works, and if you close/reopen the solution the errors go away.
Unchecking the `Run code analysis in a separate process`, also seems
to solve the issue.

.NET MAUI seems to be using an intersection of features that causes
this problem:

1. We have a `dotnet new maui` project template.
2. We have a source generator in a NuGet package.
3. We have MSBuild targets that setup the source generator in a NuGet
   package.
4. We use `AdditionalFiles` to pass `.xaml` files in to the source
   generator.

What happens on the first project load, the design-time builds
*before* project restore are missing the source generator. So the call
to `<Csc/>` does not have `@(Analyzer)` or `AdditionalFiles` at all.

To solve this issue, we can move everything *out* of NuGet packages,
so they become regular "SDK" or workload packs:

* `Microsoft.Maui.Controls.Build.Tasks` is no longer packable
    * all files moved to `Microsoft.Maui.Sdk`
* `Resizetizer.csproj` is no longer packable
    * actual files are in `Microsoft.Maui.Resizetizer.Sdk`
* `$(MauiVersion)` no longer applies to build tasks

Testing locally, this seems to solve the problem for me. I can see the
`@(Analyzer)` and `AdditionalFiles` being passed on all design-time
builds now.

Other changes:

* I added `%(IsImplicitlyDefined)` to the source generator assembly. I
  just noticed this was missing while reading `.binlog` files.
* I put all files in the `Sdk` folder in `Microsoft.Maui.Sdk`. This
  seems a bit simpler to have one directory.
Repository owner moved this from In Progress to Done in [ARCHIVED] MAUI Planning Jan 21, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Feb 20, 2022
@samhouts samhouts added the fixed-in-6.0.200-preview.13.2 Look for this fix in 6.0.200-preview.13.2! label Jul 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-tooling XAML & C# Hot Reload, XAML Editor, Live Visual Tree, Live Preview, Debugging area-xaml XAML, CSS, Triggers, Behaviors fixed-in-6.0.200-preview.13.2 Look for this fix in 6.0.200-preview.13.2! t/bug Something isn't working
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants