Code Analysis Code Analysis
Code Analysis Code Analysis
DocFX + Singulink = ♥

Search Results for

    Sharing Your Library

    If your library's public API has non-copyable structs, let the analyzer reach the projects that use your library, so that they get a warning when they copy your structs. This page shows how, and how to ship a symbol list file with the library.

    At a Glance

    • Reference the analyzers package with PrivateAssets="none", or reference Singulink.CodeAnalysis as a normal reference: either one passes the analyzer on.
    • The analyzer then reaches every project down the chain, and checks there only what your library marked. In those projects, it does not check the ten built-in framework structs.
    • A library that keeps the analyzer to itself leaves the projects that use it without the analyzer, and a project that references both gets a warning about it (SCA1025).
    • A symbol list file can ship with the library, for the members of its own generics that are not marked, and for each class of the library that clones itself.

    Passing the Analyzer On

    <PackageReference Include="Singulink.CodeAnalysis.Analyzers" Version="0.7.5" PrivateAssets="none" />
    

    Your package then depends on the analyzers package with all its assets (<dependency id="Singulink.CodeAnalysis.Analyzers" version="0.7.5" include="All" />), and a project that references your package gets the analyzer. If you reference Singulink.CodeAnalysis instead, keep it a normal reference and the same holds: the attributes package already passes the analyzers on.

    Down the Chain

    The analyzer also reaches projects further down the chain. A project that references a package that depends on your package gets the analyzer too, whatever that package's own reference to yours says.

    The .NET SDK (checked with the .NET 10 and 11 SDKs) applies the analyzers of every package in the restore graph. It does not honor an exclude="Analyzers" in a dependency or ExcludeAssets="analyzers" on a reference. The only way to keep the analyzer from flowing past your library is PrivateAssets="all", which leaves the dependency out of your package altogether.

    What Your Users See

    In a project that only gets the analyzer through your library, the analyzer checks the types that your library marked, and it does not check the built-in types. See Why Am I Seeing This?, which is the page your users land on.

    Caution

    A library that keeps the analyzer to itself leaves the projects that use it without the analyzer. Their code can copy your structs where it implements one of your interfaces, makes a delegate that you call or reads an object of one of your classes, and neither your library nor a project further down the chain can see that. A project that references both your library and such an assembly gets a warning about it, once for each such assembly (SCA1025).

    Shipping a Symbol List File With a Library

    Marking the type parameters of the library's generics comes first, since the analyzer then checks their bodies: see Marking a Type Parameter. A library whose generics stay unmarked can ship what its author knows about them, so that the projects that use the library get those entries with no setup. Such a file has entries only for the library's own members and types, never for the framework or for other libraries.

    The package carries a symbol list file that is named after the library. A props file named after the package adds the file to the additional files of each project that references the package:

    <!-- build\MyLibrary.props -->
    <Project>
      <ItemGroup>
        <AdditionalFiles Include="$(MSBuildThisFileDirectory)NonCopyableSymbols.MyLibrary.txt" Visible="false" />
      </ItemGroup>
    </Project>
    
    <!-- MyLibrary.csproj -->
    <ItemGroup>
      <AdditionalFiles Include="NonCopyableSymbols.MyLibrary.txt" />
      <None Include="NonCopyableSymbols.MyLibrary.txt" Pack="true" PackagePath="build;buildTransitive" />
      <None Include="build\MyLibrary.props" Pack="true" PackagePath="build;buildTransitive" />
    </ItemGroup>
    
    Part Why
    build and buildTransitive With both, a project that references the package gets the file, and so does a project that gets the package only through another package
    The AdditionalFiles line of the library's own project file The library reads its own file, so its author sees a line that is wrong (see below)
    Visible="false" Keeps the file out of the project tree of the user
    The name NonCopyableSymbols.MyLibrary.txt Keeps the file apart from the project's own NonCopyableSymbols.txt and from the file of every other library

    The AdditionalFiles line makes the library read its own file, wherever the file is. So a line that cannot be parsed, or that does not fit what it names, gets a warning in the library's own build (SCA1042), where its author sees it. In a project that uses the library, such a line gets a warning at the file in the package. The exception is a line that names a member of the built-in framework symbol list for more type parameters than that list does: only the library's own build warns about it, and the line has no effect anywhere.

    For the project that uses the library, the file is a symbol list file like its own: all symbol list files are merged, with each other and with the built-in framework symbol list. A library that ships a symbol list file also passes the analyzer on, since the list is of no use without it.

    A Class That Clones Itself

    A class of the library that clones itself with MemberwiseClone, and that other projects may derive from, gets SCA1008 in the library's own build. The message of SCA1008 gives a clones line for the class. With that line in the shipped file, a class that derives from it in another project gets a warning for each field that holds a non-copyable value (SCA1009).

    Once the class has its line, SCA1008 also finds the members of the class that another project can call and that clone their object themselves, and asks for a line for each. With those lines, a project that calls such a member on an object of a derived class gets a warning there. The warnings are a help, not a proof: add a line as well for a member that clones its object through a private helper or a static helper, and for one that hands its object to other code, such as one that stores it in another object that it returns. A line for a private or internal member has no effect in the project that uses the library.

    Once the lines name every member that clones the object or hands it on, write the type line as clones=complete: a project that uses the library then gets no warning for the other members. The shipped file then has lines such as these:

    # NonCopyableSymbols.MyLibrary.txt
    T:MyLibrary.Options;clones=complete
    M:MyLibrary.Options.Copy;clones
    

    Then suppress SCA1008 on the call: see SCA1008.

    What the lines of the file can say: Symbol List.

    © Singulink. All rights reserved.