Skip to content

[Spec] Warnings and errors in the assets file

Ankit Mishra edited this page May 12, 2017 · 12 revisions

Restore and Build generate duplicate warnings and errors in some scenarios. To improve this restore should log messages to the assets file where they can be de-duplicated with build.

Adding messages to the assets file also improves the scenario where a user builds in a different session, possibly days after doing the restore. If the restore failed build could display the original failure messages from restore to help the user debug the problem.

Reading log messages example

var reader = new LockFileFormat();
var assetsFile = reader.Read(@"c:\myproject\obj\project.assets.json");

foreach (var log in assetsFile.LogMessages)
{
  Console.WriteLine($"{log.Code}: {log.Message}");
}

Log message interface

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using NuGet.Common;

namespace NuGet.ProjectModel
{
    public interface IAssetsLogMessage
    {
        /// <summary>
        /// Level to indicate if this is an error or wanring.
        /// </summary>
        LogLevel Level { get; }

        /// <summary>
        /// Indicates the NuGet error code.
        /// </summary>
        NuGetLogCode Code { get; }

        /// <summary>
        /// Indicates the staring generated by the code to go with the error code.
        /// </summary>
        string Message { get; }

        /// <summary>
        /// Indicates the project for which the error was thrown.
        /// </summary>
        string ProjectPath { get; }

        /// <summary>
        /// Level to indicate the warning level for the message.
        /// This is relevant only if the Level == LogLevel.Warning.
        /// </summary>
        WarningLevel WarningLevel { get; }

        /// <summary>
        /// Indicates the file for which the error was thrown.
        /// </summary>
        string FilePath { get; }

        /// <summary>
        /// Indicates the starting line for which the error was thrown.
        /// </summary>
        int StartLineNumber { get; }

        /// <summary>
        /// Indicates the starting column for which the error was thrown.
        /// </summary>
        int StartColumnNumber { get; }

        /// <summary>
        /// Indicates the ending line for which the error was thrown.
        /// </summary>
        int EndLineNumber { get; }

        /// <summary>
        /// Indicates the ending column for which the error was thrown.
        /// </summary>
        int EndColumnNumber { get; }

        /// <summary>
        /// Project or Package Id
        /// </summary>
        string LibraryId { get; }

        /// <summary>
        /// List of TargetGraphs
        /// </summary>
        IReadOnlyList<string> TargetGraphs { get; }
    }
}

Assets file example

{
  "logs": [
    {
      "code": "NU1000",
      "level": "Error",
      "filePath": "kung\\fu\\fighting.targets",
      "startLineNumber": 11,
      "startColumnNumber": 2,
      "endLineNumber": 10,
      "message": "test log message",
      "targetGraph": [
        "net46",
        "netcoreapp1.0",
        "netstandard1.6"
      ]
    }
  ]
}

Helper methods

The following are helpers that can be used to get the actual target graphs that apply to a log message, and also the library entries from the assets file that the log message refers to.

Using these the caller can access the id/version of the package and also the framework and RID of of the target graph.

/// <summary>
/// Get target graphs for the current log message.
/// </summary>
public static IEnumerable<LockFileTarget> GetTargetGraphs(this IAssetsLogMessage message, LockFile assetsFile);

/// <summary>
/// Get the library from each target graph it exists in.
/// </summary>
public static IEnumerable<LockFileTargetLibrary> GetTargetLibraries(this IAssetsLogMessage message, LockFile assetsFile);

/// <summary>
/// Get the library by id from the target graph.
/// </summary>
public static LockFileTargetLibrary GetTargetLibrary(this LockFileTarget target, string libraryId);

Contributing

What's Being Worked On?

Check out the proposals in the accepted & proposed folders on the repository, and active PRs for proposals being discussed today.

Common Problems

Clone this wiki locally