Skip to content

Consider task analyzer/fixer advising helper methods to take AbsolutePath #14816

Description

@rainersigwald

Consider a task implementation that uses a helper method, like this trivial one:

public class MyTask : Task
{
    [Required]
    public string Input {get; set;}

    public bool Execute()
    {
        return Helper(Input);
    }

    private static bool Helper(string input)
    {
        return File.Exists(input);
    }
}

When making this multithread-aware, we'd like to get an MSBuildTask003 at the File.Exists call, because we need to use a thread-aware path instead of relying on CWD. But the straightforward fixer (wrap to File.Exists(TaskEnvironment.GetAbsolutePath(input))) won't work, because the TaskEnvironment property is not available in static context.

But there's another possible fix, which is "make the static method take an AbsolutePath and push creating it elsewhere":

+ [MSBuildMultiThreadableTask]
  public class MyTask : Task
  {
      [Required]
      public string Input {get; set;}

      public bool Execute()
      {
-         return Helper(Input);
+         return Helper(TaskEnvironment.GetAbsolutePath(Input));
      }

-     private static bool Helper(string input)
+     private static bool Helper(AbsolutePath input)
      {
          return File.Exists(input);
      }
  }

This isn't necessarily applicable everywhere but would be a nice suggestion in many cases.

Ideally it could also be used to build a barrier between "MSBuild task" code and "helper code" where we could establish guarantees about the absolute-ness of paths passed to "helper code" and not require that the helpers internally use AbsolutePath, since they may be shared with another non-MSBuild use case.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions