Skip to content

NullReferenceException at DriveItemRequestBuilderExtensions.ItemWithPath (packaged WinUI 3 app) #2896

Open
@kinex

Description

@kinex

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:waiting-for-triageAn issue that is yet to be reviewed or assignedtype:bugA broken experience

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions