Code scanning for Visual Studio projects automatedly inspects your source code for security vulnerabilities, coding errors, and compliance issues using static application security testing (SAST). It integrates directly into your development workflow—often via GitHub Actions or Visual Studio’s built-in tools—so you catch problems early without leaving the IDE. To get started, you need a Visual Studio project (C#, VB.NET, or C++), an account on a platform that supports code scanning (like GitHub), and basic familiarity with Git version control. No extra software is required if you use GitHub’s free tier, though you may install extensions for deeper analysis.
What You’ll Need
- A Visual Studio project (Visual Studio 2019 or later recommended)
- A GitHub account (free tier supports code scanning for public repos; private repos require GitHub Pro or Team plan)
- Git installed and configured with your GitHub account
- Optional: SonarQube or SonarCloud account for enhanced analysis (free for open-source projects)
Step-by-Step
- Prepare your project in Git: Open your solution in Visual Studio, ensure all code is committed and pushed to a GitHub repository. If you haven’t initialized a repo, right-click the solution in Solution Explorer > Add > Add to Source Control. Then push to GitHub via Team Explorer > Sync > Push.
- Enable code scanning in GitHub: Go to your repository on GitHub.com. Click Settings > Code security and analysis. Under Code scanning alerts, toggle Set up code scanning to On. Select GitHub code scanning as the tool (this uses Semmle/CodeQL).
- Configure the analysis workflow: GitHub will prompt you to create a workflow file (`.github/workflows/codeql-analysis.yml`). Choose Add a workflow. For most .NET projects, accept the default settings. The workflow runs on every push to
mainand pull requests, scanning C#, VB.NET, and JavaScript/TypeScript (if present). Click Start commit > Commit new file. - Run the initial scan: After committing the workflow, go to the Actions tab. Wait for the CodeQL Analyze workflow to complete (typically 2–5 minutes). If it succeeds, you’ll see a green checkmark.
- Review and triage alerts: Navigate to Security > Code scanning alerts. Here, GitHub lists findings grouped by severity (error, warning, recommendation). Each alert includes the affected file, line number, a description, and a link to the CodeQL query explanation. Click an alert to view the full context and suggested fix.
- Fix and re-scan: Edit the code in Visual Studio to address the issue (e.g., fix a null dereference or SQL injection risk), commit, and push. The next workflow run will auto-update the alert status. Mark resolved alerts as Dismissed in GitHub if no longer relevant.
Common Mistakes
Skipping the Git push before enabling code scanning—GitHub scans only code already in the repository, not local changes. Another frequent error is selecting the wrong branch in the workflow file (e.g., master instead of main), causing scans to run on outdated code. Also, many forget to grant GitHub Actions permissions: after enabling code scanning, verify Write permissions are enabled under Settings > Actions > General > Workflow permissions (set to Read and write permissions).
Tips
Use CodeQL query suites to balance thoroughness and speed. In the workflow file, replace security-extended with security-and-quality for stricter rules (but longer scans). For large solutions, exclude test projects by adding paths-ignore to the workflow YAML (e.g., paths-ignore: ['**/Tests/**']). If your project uses .NET 6+ or .NET 8, ensure the workflow uses setup-dotnet action with the correct SDK version to avoid missing dependencies during analysis.
Troubleshooting
If the workflow fails, check the Actions tab logs. Common issues: Missing dependencies (e.g., NuGet packages not restored)—add dotnet restore before codeql database create in the workflow. Permissions denied—re-authenticate GitHub CLI or check repository access tokens. No alerts but you expect some—verify the workflow runs on the right branch and that your code includes typical vulnerabilities (e.g., hardcoded credentials). For Visual Studio-specific errors, rebuild the solution first to ensure binaries are generated, as some analyzers require compiled outputs.
Frequently Asked Questions
Does code scanning work with on-premises TFS/Azure DevOps?
Not natively. GitHub code scanning requires GitHub.com or GitHub Enterprise Cloud. For Azure DevOps, use the built-in Code Analysis (via FxCop analyzers) or integrate SonarQube via extensions.
Can I scan C++ projects?
Yes. GitHub’s CodeQL supports C++ analysis out of the box. Ensure your workflow includes init: { tools: [cpp] } or let the default auto-detect C++ files. For complex builds, you may need to specify compiler commands in the workflow.
How often does code scanning run by default?
Every time you push to the default branch or create a pull request. You can customize this in the workflow’s on: section (e.g., add schedules: for nightly scans).
Are alerts shared across team members?
Yes. All repository maintainers and collaborators with Read access can view alerts. Assignees and dismissals are visible to everyone, enabling team-wide remediation tracking.
Can I disable specific alerts permanently?
Yes. In the alert view, click Dismiss > choose a reason (e.g., False positive). For recurring false positives, add a # codeql[!warning] comment in the code or exclude the rule in the workflow using queries with - prefixes (e.g., - security-extended).
Does this replace unit testing?
No. Code scanning focuses on security and correctness patterns (e.g., buffer overflows, injection flaws), while unit tests verify logic behavior. Use both: run code scanning for proactive risk detection and tests for functional validation.