Avoid referencing source file out of the project directory
Matches types whose source files are outside the Visual Studio project file's directory (and sub-directories). Such files, added via > Add > Existing Items… > Add As Link, share type definitions across assemblies, duplicating types at binary level: degraded maintainability and subtle versioning bugs.
Tolerable for some types shared across executables, e.g. startup concerns like assembly resolving handlers or .NET Framework version checks.
Tolerable for some types shared across executables, e.g. startup concerns like assembly resolving handlers or .NET Framework version checks.
How to fix
Reference from a VS project only source files under the project file location. Severity is Low: more an advice than a problem.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Avoid referencing source file out of the project directory</Name>
// <Id>ND2100:AvoidReferencingSourceFileOutOfVisualStudioProjectDirectory</Id>
warnif count > 0
from a in Application.Assemblies
where a.VisualStudioProjectFilePath != null
let vsProjDirPath = a.VisualStudioProjectFilePath.ParentDirectoryPath
let vsProjDirPathStr = vsProjDirPath.ToString()
from t in a.ChildTypes
where JustMyCode.Contains(t) && t.SourceFileDeclAvailable
from decl in t.SourceDecls
let sourceFilePath = decl.SourceFile.FilePath
let sourceFilePathStr = sourceFilePath.ToString()
where sourceFilePathStr.IndexOf(vsProjDirPathStr, StringComparison.OrdinalIgnoreCase) != 0 &&
// Check that both absolute paths are on same volume before emitting an issue
sourceFilePath.OnSameVolumeThan(vsProjDirPath)
select new {
t,
sourceFilePath,
projectFilePath = a.VisualStudioProjectFilePath,
Debt = 10.ToMinutes().ToDebt(),
Severity = Severity.Low
}
//<Expl>
// The source file *{1}* is referenced by the project file *{2}*
// but is located out of the project's directory or children directories.
//</Expl>
//<Desc>
// Matches types whose source files are outside the Visual Studio project
// file's directory (and sub-directories). Such files, added via
// *> Add > Existing Items… > Add As Link*, share type definitions across
// assemblies, duplicating types at binary level: degraded maintainability
// and subtle versioning bugs.
//
// Tolerable for some types shared across executables, e.g. startup concerns
// like assembly resolving handlers or .NET Framework version checks.
//</Desc>
//<HowToFix>
// Reference from a VS project only source files under the project file
// location. Severity is **Low**: more an advice than a problem.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Source Files Organization: all 6 items
- Avoid referencing source file out of the project directory ND2100
- Avoid duplicating a type definition across assemblies ND2101
- Avoid defining multiple types in a source file ND2102
- Namespace name should correspond to file location ND2103
- Types with source files stored in the same directory, should be declared in the same namespace ND2104
- Types declared in the same namespace, should have their source files stored in the same directory ND2105