I have hit an intermittent (hard to reproduce) issue when building a particular target with MSVC. I've added custom versions of msvc.link and msvc.compile for /clr and .NET support. They've been working fine for the few targets I originally developed them for. This problem started happening when I recently added another target using these custom versions. The symptom is that I get this link command with doubled up "link" and flags suggesting the $(.LD) and $(LINKFLAGS) variables are getting doubled up. It's so hard to reproduce I haven't managed to get any --debug-configuration output when it happens. I've only gotten it to happen on our AWS build agents. I was hoping one of the veterans would have a hunch about what's happening just from the symptom.

 link link /NOLOGO /NOLOGO /INCREMENTAL:NO /INCREMENTAL:NO /DLL /DEBUG /DEBUG /MACHINE:X64 /MACHINE:X64 /MANIFEST /MANIFEST /subsystem:console /subsystem:console /out:"Z:\path\to\PrmPasefScheduler.dll" /LIBPATH:"Z:\some\path\x64" /LIBPATH:"Z:\some\other\path\x64"  /MACHINE:x64 /FIXED:No /MACHINE:x64 /FIXED:No @"Z:\path\to\PrmPasefScheduler.dll.rsp"

Just in case you want to dig deeper, here is the problematic target:

rule msvc-requirement ( properties * )
{
    if ! <toolset>msvc in $(properties) { return <build>no ; }
}
obj PrmScheduler       : PrmScheduler.cpp        : <conditional>@msvc-requirement <define>UNICODE <define>_UNICODE <toolset>msvc:<using-clr>true ;

rule prmscheduler-requirements ( properties * )
{
    if <address-model>64 in $(properties)
    {
        return <search>x64 ;
    }
    else
    {
        return <search>x86 ;
    }
}

rule prmscheduler-usage-requirements ( properties * )
{
    if <address-model>64 in $(properties)
    {
        return <assembly-dependency>$(PRMSCHEDULER_CLI_ROOT)/x64/prmscheduler.dll ;
    }
    else
    {
        return <assembly-dependency>$(PRMSCHEDULER_CLI_ROOT)/x86/prmscheduler.dll ;
    }
}

searched-lib prmscheduler : : <conditional>@prmscheduler-requirements : : <conditional>@prmscheduler-usage-requirements ;

constant PLATFORM : "x64" ;
lib PrmPasefScheduler
    : # sources
        PrmScheduler
    : # requirements
        <library>prmscheduler
        <link>shared
        <library>$(PWIZ_ROOT_PATH)/pwiz/utility/misc//pwiz_utility_misc/<link>static/<using-clr>false
        <linkflags>"/MACHINE:$(PLATFORM) /FIXED:No"
        <cxxflags>/permissive
        <conditional>@msvc-requirement
       <define>UNICODE
       <define>_UNICODE
       <toolset>msvc:<using-clr>true
    : # default-build
    : # usage-requirements
        <library>prmscheduler
        <library>$(PWIZ_ROOT_PATH)/pwiz/utility/misc//pwiz_utility_misc/<link>static/<using-clr>false
    ;
   

Here are the custom rules/actions:

actions compile-c++clr
{
    $(.SETUP) $(.CC) @"@($(<[1]:W).rsp:E="$(>[1]:W)" -Fo"$(<[1]:W)" -Yu"$(>[3]:D=)" -Fp"$(>[2]:W)" /clr $(ASSEMBLY_INCLUDE_PATHS) $(ASSEMBLIES) $(CC_RSPLINE))" $(.CC.FILTER)" > "$(<[1]:W).out"
    set status=%ERRORLEVEL%
    findstr /V /X "$(>[1]:D=)" "$(<[1]:W).out"
    exit %status%
}

rule compile.c++clr ( targets + : sources * : properties * )
{
    set-setup-command $(targets) : $(properties) ;
    set-assemblies $(<[1]) : $(properties) ;
    get-rspline $(targets) : -TP ;
    compile-c++clr $(<) : $(>) [ on $(<) return $(PCH_FILE) ] [ on $(<) return $(PCH_HEADER) ] ;
}

actions link.clr.dll bind DEF_FILE LIBRARIES_MENTIONED_BY_FILE
{
    $(.SETUP) $(.LD) /DLL $(LINKFLAGS) /out:"$(<[1]:W)" /LIBPATH:"$(LINKPATH:W)" /def:"$(DEF_FILE)" $(OPTIONS) @"@($(<[1]:W).rsp:E=$(.nl)"$(>)" $(.nl)$(LIBRARIES_MENTIONED_BY_FILE) $(.nl)$(LIBRARIES) $(.nl)"$(LIBRARY_OPTION)$(FINDLIBS_ST).lib" $(.nl)"$(LIBRARY_OPTION)$(FINDLIBS_SA).lib")" $(MANIFEST_DEPENDENCIES)
    if %ERRORLEVEL% NEQ 0 EXIT %ERRORLEVEL%
}

rule link.clr.dll ( targets + : sources * : properties * )
{
    set-setup-command $(targets) : $(properties) ;
    set-manifest-dependencies $(<[1]) : $(properties) ;
    DEPENDS $(<) : [ on $(<) return $(DEF_FILE) ] ;
    if <embed-manifest>on in $(properties)
    {
        msvc.manifest.dll $(targets) : $(sources) : $(properties) ;
    }
    copy-assemblies $(<[1]) : $(properties) ;
}


Here are the custom features:

feature.feature assembly            :                       : free dependency ;
feature.feature assembly-dependency :                       : free dependency ;
feature.feature assembly-dependency-ex :                    : free ;
feature.feature manifest-dependency :                       : free dependency ;
feature.feature configuration-file  :                       : free dependency ;
feature.feature using-clr           : false true            : composite ;
feature.compose <using-clr>true     : <asynch-exceptions>on <runtime-link>shared ;

generators.register [ new msvc-linking-generator    msvc.link.clr.dll : OBJ SEARCHED_LIB STATIC_LIB : SHARED_LIB : <toolset>msvc <using-clr>true ] ;
generators.override msvc.link.clr.dll : msvc.link.dll ;

generators.register-c-compiler msvc.compile.c++clr : CPP : OBJ : <toolset>msvc <using-clr>true ;



Here are some utility functions used in the above actions:

#
# Add /FU and /AI flags for targets using /clr
#
rule set-assemblies ( target : properties * )
{
    local assemblies-string ;
    local assemblies = [ feature.get-values <assembly> : $(properties) ] ;
    if $(assemblies)
    {
        for local assembly in $(assemblies)
        {
            local assembly-path = [ $(assembly).name ] ;
            if $(assembly-path:S) != ".pdb"
            {
                if ! [ path.is-rooted $(assembly-path) ]
                {
                    assembly-path = [ path.join [ $(assembly).path ] $(assembly-path:D=) ] ;
                }
                assembly-path = [ path.native $(assembly-path) ] ;
                assemblies-string += " /FU\"$(assembly-path)\"" ;
            }
        }
    }
    ASSEMBLIES on $(target) = $(assemblies-string) ;

    # add /AI flags for each assembly-dependency's (unique) directory
    local assembly-include-string ;
    local assembly-dependencies = [ feature.get-values <assembly-dependency> : $(properties) ] ;
    if $(assembly-dependencies)
    {
        local assembly-include-paths ;
        for local dependency in $(assembly-dependencies)
        {
            local dependency-path = [ $(dependency).name ] ;
            if ! [ path.is-rooted $(dependency-path) ]
            {
                dependency-path = [ path.join [ $(dependency).path ] $(dependency-path:D=) ] ;
            }
            dependency-path = [ path.native $(dependency-path) ] ;
            dependency-path = $(dependency-path:D) ;
            if ! $(dependency-path) in $(assembly-include-paths)
            {
                assembly-include-paths += $(dependency-path) ;
                assembly-include-string += " /AI\"$(dependency-path)\"" ;
            }
        }
    }
    ASSEMBLY_INCLUDE_PATHS on $(target) = $(assembly-include-string) ;
}

#
# Add /MANIFESTDEPENDENCY flags (primarily for side-by-side loading of COM DLLs)
#
local rule set-manifest-dependencies ( target : properties * )
{
    local manifest-dependencies-string ;
    local manifest-dependencies = [ feature.get-values <manifest-dependency> : $(properties) ] ;
    if $(manifest-dependencies)
    {
        # type='win32' name='<assembly_basename>' version='<assembly_version>'
        for local dependency in $(manifest-dependencies)
        {
            local dependency-path = [ path.native [ $(dependency).name ] ] ;
            local dependency-version = [ MATCH "FileVersion:     ([0-9\\.]+)" : [ SHELL "ShowVer.exe $(dependency-path)" ] ] ;
           
            # unknown version will cause a runtime-error; is there a better solution?
            dependency-version ?= "unknown" ;
           
            # the name of the manifest can not match the basename of the DLL because of a Windows XP loader bug
            manifest-dependencies-string += " /MANIFESTDEPENDENCY:\"type='win32' name='$(dependency-path:B).SxS' version='$(dependency-version)'\"" ;
        }
    }
    MANIFEST_DEPENDENCIES on $(target) = $(manifest-dependencies-string) ;
}

#
# Copy .NET assemblies and their native DLL dependencies to the target's directory;
# Also .NET applications may need a side-by-side configuration file that must be named like $(target).exe.config - copy that if necessary
#
rule copy-assemblies ( target : properties * )
{
    local assemblies = [ feature.get-values <assembly> : $(properties) ] ;
    assemblies += [ feature.get-values <assembly-dependency> : $(properties) ] ;
    if $(assemblies)
    {
        local target-path = [ on $(target) return $(LOCATE) ] ;
        for local assembly in $(assemblies)
        {
            local assembly-path = [ $(assembly).name ] ;
            if ! [ path.is-rooted $(assembly-path) ]
            {
                assembly-path = [ path.join [ $(assembly).path ] $(assembly-path:D=) ] ;
            }
            assembly-path = [ path.native $(assembly-path) ] ;
            local assembly-at-target-filepath = [ path.native [ path.join $(target-path) $(assembly-path:D=) ] ] ;

            if $(assembly-at-target-filepath:L) != $(assembly-path:L) &&
               ! ( $(assembly-at-target-filepath:L) in $(.unique-targets) )
            {
                .unique-targets += $(assembly-at-target-filepath:L) ;
                if ! [ CHECK_IF_FILE $(assembly-at-target-filepath:L) ]
                {
                    common.hard-link $(assembly-at-target-filepath) : $(assembly-path) ;
                    DEPENDS $(assembly-at-target-filepath) : $(target-path) [ $(assembly).actualize ] ;
                    DEPENDS $(target) : $(assembly-at-target-filepath) ;
                    #JAM_SEMAPHORE on $(target-filepath) = "dotNetSemaphore" ;
                    #JAM_SEMAPHORE on $(target) = "dotNetSemaphore" ;
                }
            }
        }
    }

    # .NET applications may need a side-by-side configuration file that must be named like $(target).exe.config
    local configuration-file = [ feature.get-values <configuration-file> : $(properties) ] ;
    if $(configuration-file)
    {
        local target-path = [ on $(target) return $(LOCATE) ] ;
        local configuration-file-path = [ $(configuration-file).name ] ;
        if ! [ path.is-rooted $(configuration-file-path) ]
        {
            configuration-file-path = [ path.join [ $(configuration-file).path ] $(configuration-file-path:D=) ] ;
        }
        configuration-file-path = [ path.native $(configuration-file-path) ] ;
        local target-filepath = [ path.native [ path.join $(target-path) $(configuration-file-path:D=) ] ] ;
        if $(target-filepath) != $(configuration-file-path) &&
           ! ( $(configuration-file-path) in $(.unique-targets) )
        {
            .unique-targets += $(configuration-file-path) ;
            common.hard-link $(target-filepath) : $(configuration-file-path) ;
            DEPENDS $(target-filepath) : $(target-path) ;
            DEPENDS $(target) : $(target-filepath) ;
            #JAM_SEMAPHORE on $(target-filepath) = "dotNetSemaphore" ;
            #JAM_SEMAPHORE on $(target) = "dotNetSemaphore" ;
        }
    }
}