microsoftgraph / microsoftgraph/msgraph-sdk-dotnet

NullReferenceException at DriveItemRequestBuilderExtensions.ItemWithPath (packaged WinUI 3 app)

Open
#2,896 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:bug
Dominant language
C#
Stars
789
Forks
264
Avg merge
15h 17m
Merged PRs (30d)
3

Description

Describe the bug

This code throws NullReferenceException in a packaged WinUI 3 app (so maybe something related to Native AOT):

var driveItem = _graphServiceClient.Drives[driveId].Items[itemId].ItemWithPath(relativePath).GetAsync();

Crash:

 System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Graph.DriveItemRequestBuilderExtensions.ItemWithPath(DriveItemItemRequestBuilder, String) 
Expected behavior

No crash

How to reproduce
  1. Create a WinUI 3 app
  2. Add some code to call ItemWithPath, for example:
    var driveItem = _graphServiceClient.Drives[driveId].Items[itemId].ItemWithPath(relativePath).GetAsync();
  3. Add to .csproj:
    <GenerateAppxPackageOnBuild>true</GenerateAppxPackageOnBuild>
  4. Start debugging (release build)
SDK Version

5.77.0

Latest version known to work for scenario above?

No response

Known Workarounds

Do not use .ItemWithPath, for example:

        private async Task<DriveItem> GetItemAsync(string relativePath)
        {
            // Normalize the path to use forward slashes
            relativePath = relativePath.Replace('\\', '/');

            var pathParts = relativePath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            var currentItem = await GraphServiceClient
                .Drives[DriveId]
                .Items[Id]
                .GetAsync().ConfigureAwait(false);

            foreach (var part in pathParts)
            {
                var children = await GraphServiceClient
                    .Drives[DriveId]
                    .Items[currentItem.Id]
                    .Children
                    .GetAsync().ConfigureAwait(false);

                currentItem = children.Value
                    .FirstOrDefault(item => item.Name.Equals(part, StringComparison.OrdinalIgnoreCase));

                if (currentItem == null)
                {
                    throw new FileNotFoundException($"Item '{part}' not found in the path.");
                }
            }

            return currentItem;
        }

Debug output

No response

Configuration
  • WIndows 11
  • arm64
Other information

I added some logging and noticed that in this code the variable field will be null which is not correct (it has a valid value in a debug build):
https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/main/src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs#L95

I see there also these build warnings:

Assembly 'Microsoft.Kiota.Serialization.Json' produced trim warnings. For more information see https://aka.ms/il2104
Assembly 'Microsoft.Graph' produced trim warnings. For more information see https://aka.ms/il2104

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs around line 95, where the packaged release build reports that field is null. Reproduce the ItemWithPath call in a packaged WinUI 3 arm64 release build and inspect the Microsoft.Graph and Microsoft.Kiota.Serialization.Json trim warnings. Done means ItemWithPath no longer throws in this scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.