# Sourcegraph Releases

This page displays the release plan of all Sourcegraph versions, including deprecation schedules, links to relevant release notes, and installation links.

Sourcegraph releases new features and bug fixes via feature and patch releases regularly. We support the two most recent major releases of Sourcegraph ([more details here](https://www.notion.so/Sourcegraph-Releases-eee2a5384b0a4555adb51b439ddde35f?pvs=21)). See the link to the corresponding release notes for more information on each release. For more information about the release process, please see our [Sourcegraph releases process documentation](https://www.notion.so/Sourcegraph-Releases-eee2a5384b0a4555adb51b439ddde35f?pvs=21). ## Supported Releases Currently supported versions of Sourcegraph: | **Release** | **General Availability Date** | **Supported** | **Release Notes** | **Install** | |--------------|-------------------------------|---------------|--------------------------------------------------------------------|------------------------------------------------------| | 6.4 Patch 3 | June 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v643889) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.4 Patch 2 | June 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v642622) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.4 Patch 1 | June 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v641203) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.4 Patch 0 | May 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v640) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.3 Patch 2 | May 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v634167) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.3 Patch 1 | May 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v632692) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.3 Patch 0 | April 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v630) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.2 Patch 3 | April 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v623841) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.2 Patch 2 | April 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v622553) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.2 Patch 1 | April 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v621106) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.2 Patch 0 | April 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v620) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.1 Patch 5 | March 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v615633) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.1 Patch 4 | March 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v614020) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.1 Patch 3 | March 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v612889) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.1 Patch 2 | February 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v611295) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.1 Patch 1 | February 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v61376) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.1 Patch 0 | February 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v610) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.0 Patch 2 | April 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v6012741) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.0 Patch 1 | February 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v602687) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 6.0 Patch 0 | January 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v600) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.11 Patch 5 | January 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5116271) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.11 Patch 4 | January 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5114013) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.11 Patch 3 | January 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5114013) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.11 Patch 2 | January 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5113601) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.11 Patch 1 | January 2025 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5112732) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.11 Patch 0 | December 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5110) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.10 Patch 3 | December 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5103940) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.10 Patch 2 | December 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5102832) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.10 Patch 1 | December 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5101164) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.10 Patch 0 | November 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5100) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.9 Patch 3 | November 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v591590) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.9 Patch 2 | November 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v59347) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.9 Patch 1 | November 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5945) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.9 Patch 0 | October 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v590) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.8 Patch 1 | October 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v581579) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.8 Patch 0 | October 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v580) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.7 Patch 1 | September 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v572474) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.7 Patch 0 | September 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v570) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.6 Patch 2 | August 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v562535) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.6 Patch 1 | August 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v56185) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.6 | August 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v560) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.5 | July 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v553956) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.4 | May 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v547765) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.3 | February 2024 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v5312303) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.2 | October 2023 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v527) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.1 | June 2023 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v519) | [Install](https://sourcegraph.com/docs/admin/deploy) | | 5.0 | March 2023 | ✅ | [Notes](https://sourcegraph.com/docs/technical-changelog#v506) | [Install](https://sourcegraph.com/docs/admin/deploy) | ## Deprecated Releases These versions fall outside the release lifecycle and are not supported anymore: | **Release** | **General Availability Date** | **Supported** | **Release Notes** | |-------------|-------------------------------|---------------|-------------------------------------------------------------------------------------------------| | 4.5 | February 2023 | ❌ | [Notes](https://sourcegraph.com/docs/technical-changelog#v451) | | 4.4 | January 2023 | ❌ | [Notes](https://sourcegraph.com/docs/technical-changelog#v442) | | 4.3 | December 2022 | ❌ | [Notes](https://sourcegraph.com/docs/technical-changelog#v431) | | 4.2 | November 2022 | ❌ | [Notes](https://sourcegraph.com/docs/technical-changelog#v421) | | 4.1 | October 2022 | ❌ | [Notes](https://sourcegraph.com/docs/technical-changelog#v413) | | 4.0 | September 2022 | ❌ | [Notes](https://sourcegraph.com/docs/technical-changelog#v401) | | 3.43 | August 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3432) | | 3.42 | July 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3422) | | 3.41 | June 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3422) | | 3.40 | May 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3402) | | 3.39 | April 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3391) | | 3.38 | March 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3391) | | 3.37 | February 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3391) | | 3.36 | January 2022 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3363) | | 3.35 | December 2021 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3352) | | 3.34 | November 2021 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3352) | | 3.33 | October 2021 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3332) | | 3.32 | September 2021 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3321) | | 3.31 | August 2021 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3321) | | 3.30 | July 2021 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3321) | | 3.29 | June 2021 | ❌ | [Notes](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/main/CHANGELOG.md#3321) |
# Legacy Versions

This page displays the docs for legacy Sourcegraph versions less than 5.1

- [6.3](https://6.3.sourcegraph.com) - [6.2](https://6.2.sourcegraph.com) - [6.1](https://6.1.sourcegraph.com) - [6.0](https://6.0.sourcegraph.com) - [5.11](https://5.11.sourcegraph.com/) - [5.10](https://5.10.sourcegraph.com/) - [5.9](https://5.9.sourcegraph.com/) - [5.8](https://5.8.sourcegraph.com/) - [5.7](https://5.7.sourcegraph.com/) - [5.6](https://5.6.sourcegraph.com/) - [5.5](https://5.5.sourcegraph.com/) - [5.4](https://5.4.sourcegraph.com) - [5.3](https://5.3.sourcegraph.com/) - [5.2](https://5.2.sourcegraph.com/) - [5.1](https://docs.sourcegraph.com/@5.1/) - [5.0](https://docs.sourcegraph.com/@5.0/) - [4.5](https://docs.sourcegraph.com/@4.5/) - [4.4](https://docs.sourcegraph.com/@4.4/) - [4.3](https://docs.sourcegraph.com/@4.3/) - [4.2](https://docs.sourcegraph.com/@4.2/) - [4.1](https://docs.sourcegraph.com/@4.1/) - [4.0](https://docs.sourcegraph.com/@4.0/) - [3.43](https://docs.sourcegraph.com/@3.43/) - [3.41](https://docs.sourcegraph.com/@3.41/) - [3.40](https://docs.sourcegraph.com/@3.40/) - [3.39](https://docs.sourcegraph.com/@3.39/) - [3.38](https://docs.sourcegraph.com/@3.38/) - [3.37](https://docs.sourcegraph.com/@3.37/) - [3.36](https://docs.sourcegraph.com/@3.36/) - [3.35](https://docs.sourcegraph.com/@3.35/) - [3.34](https://docs.sourcegraph.com/@3.34/) - [3.32](https://docs.sourcegraph.com/@3.32/) - [3.31](https://docs.sourcegraph.com/@3.31/) - [3.30](https://docs.sourcegraph.com/@3.30/) - [3.29](https://docs.sourcegraph.com/@3.29/) - [3.28](https://docs.sourcegraph.com/@3.28/) - [3.27](https://docs.sourcegraph.com/@3.27/) - [3.26](https://docs.sourcegraph.com/@3.26/) - [3.25](https://docs.sourcegraph.com/@3.25/) - [3.24](https://docs.sourcegraph.com/@3.24/) - [3.23](https://docs.sourcegraph.com/@3.23/) - [3.22](https://docs.sourcegraph.com/@3.22/) - [3.21](https://docs.sourcegraph.com/@3.21/)
Sourcegraph is a Code Intelligence platform that deeply understands your code, no matter how large or where it’s hosted, to power modern developer experiences. - **Cody**: Use Cody our AI code assistant to read, write, and understand your entire codebase faster - **Code search:** Search through all of your repositories across all branches and all code hosts - **Code intelligence:** Navigate code, find references, see code owners, trace history, and more - **Fix & refactor:** Roll out and track large-scale changes and migrations across repos at once ## Quickstart ## Main Features Some of the main Sourcegraph features include: | **Feature** | **Description** | | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | **Cody** | Cody is a free AI coding assistant that writes, fixes, and maintains your code | | **Code Navigation** | Jump-to-definition, find references, and other IDE-like code browsing features on any branch, commit, or PR/code review | | **Code Insights** | Reveal high-level information about your codebase at its current state and over time, to track migrations, version usage, vulnerability remediation, ownership, and more | | **Batch Changes** | Make large-scale code changes across many repositories and codehosts | | **Integrations** | With code hosts, code review tools, editors, web browsers, etc. | ## What's New Stay in sync and get the latest news on Sourcegraph products, technologies, and culture. [Read the blog →](https://sourcegraph.com/blog) ## Join our Community If you have questions about anything related to Sourcegraph, you're always welcome to ask our in our [Sourcegraph community forum](https://community.sourcegraph.com), [Discord](https://discord.gg/s2qDtYGnAE), and [X](https://twitter.com/sourcegraph). # Sourcegraph Tutorials

Explore to learn more about Sourcegaph tutorials and guides.

## Find Your Way Around Sourcegraph | Topic | Content Type | Description | | ---------------------------------------------------------------------------- | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | | [Sourcegraph 101](/getting-started/) | Explanation | What is Sourcegraph? Who should use it? Why do I need it? What does it do? | | [High Level Product Demo](https://www.youtube.com/watch?v=Kk1ea2-l8Hk) | Explanation (video) | A short 3-minute video describing what Sourcegraph is and how it can be useful. | | [Navigating the Sourcegraph UI](https://www.youtube.com/watch?v=6K7e74a7aC4) | Tutorial (video) | Take a look at how you can read code, find references, troubleshoot errors, gain insight, and make changes on a massive scale in Sourcegraph. | ## Get Started Searching | Topic | Content Type | Description | | -------------------------------------------------------------------------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [Three Types of Code Search](https://www.youtube.com/watch?v=-EGn_-2d9CQ) | Tutorial (video) | Code search is a vital tool for developers. It's great for digging up answers to questions from your own codebase, but it's even better for exploring and understanding code. This video will show you the types of code search we have on Sourcegraph. | | [Understanding Code Search Results](https://www.youtube.com/watch?v=oMWdYfG6-DQ) | Tutorial (video) | In this video, you'll understand the search results page and how to scope, export, save, and link to search results. | | [Basic Code Search Filters](https://www.youtube.com/watch?v=h1Kw0Wd9qZ4) | Tutorial (video) | In this video, you'll learn how to use Sourcegraph's code search filters and how they work. Filters are a great way to narrow down or search for specific code. This video covers language, repo, branch, file, and negative filters. | | [Search Query Syntax](/code-search/queries) | Reference | This page describes search pattern syntax and filters available for code search | ## More Advanced Searching | Topic | Content Type | Description | | ------------------------------------------------------------------------------------------------------------------------------------------ | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [Search Subexpressions](/code-search/working/search_subexpressions) | Tutorial | Search subexpressions combine groups of filters like `repo:` and operators like `AND` & `OR`. Compared to basic examples, search subexpressions allow more sophisticated queries. | | [Regular Expression Search Deep Dive](https://about.sourcegraph.com/blog/how-to-search-with-sourcegraph-using-regular-expression-patterns) | Tutorial | Regular expressions, often shortened as regex, help you find code that matches a pattern (including classes of characters like letters, numbers and whitespace), and can restrict the results to anchors like the start of a line, the end of a line, or word boundary. | | [Structural Search Tutorial ](https://about.sourcegraph.com/blog/how-to-search-with-sourcegraph-using-structural-patterns) | Tutorial | Structural search helps you search code for syntactical code patterns like function calls, arguments, `if...else` statements, and `try...catch` statements. It's useful for finding nested and recursive patterns as well as multi-line blocks of code. | | [Structural Search Tutorial ](https://youtu.be/GnubTdnilbc) | Tutorial (video) | Structural search helps you search code for syntactical code patterns like function calls, arguments, `if...else` statements, and `try...catch` statements. It's useful for finding nested and recursive patterns as well as multi-line blocks of code. | ## Cody | Topic | Content Type | Description | | ------------------------------------------------------------------------------------------------------------------------------------------ | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [Product Tour is VSCode](https://www.youtube.com/watch?v=OTIYXUwAQtI&t=6s) | Tutorial (video) | A video tutorial walkthrough of Cody in VSCode. | |[Cody Admin Training](https://www.youtube.com/watch?v=_Xwr7YlfTt0) | Tutorial (video) | A video tutorial explaining Cody administrative functionality.| |[Cody Tutorial in VS Code](/cody/use-cases/vsc-tutorial) | Tutorial (written) | A single documentation page with animated .gifs, demonstrating Cody features in VS Code | ## Code Navigation | Topic | Content Type | Description | | ----------------------------------------------------------------------------------------------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------- | | [Introduction to Code Navigation](/code-search/code-navigation/) | Explanation | There are 2 types of code navigation that Sourcegraph supports: search-based and precise. | | [Code Navigation Features](/code-search/code-navigation/features) | Explanation | An overview of Code Navigation features, such as "find references", "go to definition", and "find implementations". | ## Code Insights | Topic | Content Type | Description | | ------------------------------------------------------------------------------- | ------------------- | ------------------------------------------------------------------------------------------- | | [Code Insights Overview](https://www.youtube.com/watch?v=HdQIFuUzGFI) | Explanation (video) | Learn about common Code Insights use cases and see how to create an insight. | | [Quickstart Guide](/code_insights/quickstart) | Tutorial | Get started and create your first code insight in 5 minutes or less. | | [Common Use Cases](/code_insights/references/common_use_cases) | Reference | A list of common use cases for Code Insights and example data series queries you could use. | ## Batch Changes | Topic | Content Type | Description | | ----------------------------------------------------------------------------------------------------------------------------- | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | | [Introduction to Batch Changes](/batch-changes/) | Explanation | A basic introduction to the concepts, processes, and supported environments behind Batch Changes | | [Get Started With Batch Changes](https://www.youtube.com/watch?v=GKyHYqH6ggY) | Tutorial (video) | Learn how you can quickly use Sourcegraph Batch Changes to automate small and large-scale code changes server-side. | | [Batch Changes Quickstart Guide](/batch-changes/quickstart) | Tutorial | Get started and create your first batch change in 10 minutes or less. This guide follows the local (CLI) method of running batch changes. | | [Getting Started Running Batch Changes Server-Side](/batch-changes/server-side) | How-To-Guide | Follow this guide to learn how to run batch changes server-side. | ## The Sourcegraph API | Topic | Content Type | Description | | ---------------------------------------------------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [GraphQL API](/api/graphql/) | Reference | The Sourcegraph GraphQL API is a rich API that exposes data related to the code available on a Sourcegraph instance. | | [GraphQL Examples](/api/graphql/managing-search-contexts-with-api) | Reference | This page demonstrates a few example GraphQL queries for the Sourcegraph GraphQL API. | | [Streaming API](/api/stream_api/) | Reference | With the Stream API you can consume search results and related metadata as a stream of events. The Sourcegraph UI calls the Stream API for all interactive searches. Compared to our GraphQL API, it offers shorter times to first results and supports running exhaustive searches returning a large volume of results without putting pressure on the backend. | ## Search Notebooks | Topic | Content Type | Description | | --------------------------------------------------------------------------- | ------------ | ----------------------------------------------------------------------------------------------------------- | | [Search Notebooks Quickstart Guide](/notebooks/quickstart) | Tutorial | Notebooks enable powerful live–and persistent–documentation, shareable with your organization or the world. | ## Customizing Your Sourcegraph User Environment | Topic | Content Type | Description | | ------------------------------------------------------------------------------------------------ | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [Using Sourcegraph with your IDE](/integration/editor) | How-To-Guide | Sourcegraph’s editor integrations allow you search and navigate across all of your repositories without ever leaving your IDE or checking them out locally. We have built-in integrations with VS Code and JetBrains. | | [Using the Sourcegraph Browser Extension](/integration/browser_extension/) | How-To-Guide | The open-source Sourcegraph browser extension adds code navigation to files and diffs on GitHub, GitHub Enterprise, GitLab, Phabricator, Bitbucket Server and Bitbucket Data Center. | | [Using the Sourcegraph CLI](/cli/quickstart) | How-To-Guide | `src` is a command line interface to Sourcegraph that allows you to search code from your terminal, create and apply batch changes, and manage and administrate repositories, users, and more. | | [Saving Searches](/code-search/working/saved_searches) | How-To-Guide | Saved searches let you save and describe search queries so you can easily find and use them again later. You can create a saved search for anything, including diffs and commits across all branches of your repositories. | | [Search Contexts](/code-search/working/search_contexts) | How-To-Guide | Search contexts help you search the code you care about on Sourcegraph. A search context represents a set of repositories at specific revisions on a Sourcegraph instance that will be targeted by search queries by default. | ## Developer Use Cases | Topic | Content Type | Description | | ------------------------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------ | | [Writing Faster Code with Code Search and Cody](https://www.youtube.com/watch?v=vHyTgqGL41U) | Tutorial (video) | A video tutorial of how Sourcegraph can help you write code faster. | | [Generate Unit Tests](https://www.youtube.com/watch?v=FsiQMory2jI) | Tutorial (video) | A video tutorial of how to write unit tests with Cody. | | [Understand scope and impact of a code change](https://www.youtube.com/watch?v=gafdZDhSrws) | Tutorial (video) | A video tutorial of how to understand the scope and impact of a change you're considering to your codebase. |
# Sourcegraph Accounts [Sourcegraph Accounts](https://accounts.sourcegraph.com) is the centralized accounts system for all of the Sourcegraph-operated services, it provides Single Sign-On (SSO) experience for our users. The system is designed to be and operated with high-security standards to protect Personally Identifiable Information (PII), secure third-party credentials storage, abuse mitigation, and gated infrastructure access. ## FAQs ### Do Sourcegraph Enterprise users use Sourcegraph Accounts? No, Sourcegraph Enterprise users (both on Cloud and on-prem) **do not** use Sourcegraph Accounts. They continue using the SSO capability built into the Sourcegraph application. ### Do users need to recreate accounts if they previously had accounts on Sourcegraph.com? No, all of the existing Sourcegraph.com user accounts have been migrated to Sourcegraph Accounts, users should be able to use the same password (if had set) and social sign-in options (Google, GitHub, and GitLab) to sign in to Sourcegraph Accounts and get access to the same account on Sourcegraph.com. Users who had attempted to create an account but never verified their primary emails on Sourcegraph.com were not migrated. ### How to change, set or reset passwords? 1. Make sure to stay signed out on https://accounts.sourcegraph.com. 1. Visit https://accounts.sourcegraph.com/reset-password to complete the password reset. 1. Upon successful password reset, the current session is automatically signed in. ### How to delete accounts or unlink social sign-ins? Please contact Sourcegraph Support via emailing at `support@sourcegraph.com` using your primary email (shown in the https://accounts.sourcegraph.com when signed in). ### How to learn more about the technical details? For Sourcegraph teammates and whoever is interested in, please refer to [Sourcegraph Handbook](https://handbook.sourcegraph.com/departments/engineering/teams/core-services/sams/) for more technical details. ### What is Cloudflare Turnstile? [Cloudflare Turnstile](https://www.cloudflare.com/products/turnstile/) is used for application-level DDoS protection to ensure uninterrupted experience for regular users. # SLAs and Premium Support

This document explains the Sourcegraph's default contractual Service Level Agreements and Premium Support Offerings.

## Service Level Agreements (SLAs) Our service level agreements (SLAs) are designed for products that are generally available and exclude [beta and experimental features](/admin/beta_and_experimental_features). SLA response times indicate how quickly we aim to provide an initial response to your inquiries or concerns. Our team will resolve all issues as quickly as possible. However, it's important to understand that SLA times differ from guaranteed resolution times. While we always strive to respond to your issues as quickly as possible, our SLAs are specifically applicable from Monday through Friday. The following policy applies to both our cloud-based (managed instance) and on-premise/self-hosted Sourcegraph customers: ## For enterprise plans | Severity level | Description | Response time | Support availability | | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | -------------------- | | 0 | Emergency: Total loss of service or security-related issue (includes POCs) | Within two business hours of identifying the issue | 24x5 (Monday-Friday) | | 1 | Severe impact: Service significantly limited for 60%+ of users; core features are unavailable or extremely slowed down with no acceptable workaround | Within four business hours of identifying the issue | 24x5 (Monday-Friday) | | 2 | Medium impact: Core features are unavailable or somewhat slowed; workaround exists | Within eight business hours of identifying the issue | 24x5 (Monday-Friday) | | 3 | Minimal impact: Questions or clarifications regarding features, documentation, or deployments | Within two business days of identifying the issue | 24x5 (Monday-Friday) | >NOTE: Premium support with enhanced SLAs can be added to your Enterprise plans as an add-on. Our business hours, defined as Sunday 2 PM PST to Friday 5 PM PST, align with our 24x5 support coverage. ### For Enterprise Starter & Cody Pro plans | **Severity level** | **Description** | **Response time** | **Support availability** | | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------- | -------------------- | | 0 | Emergency: Total loss of service or security-related issue | Within one business day of identifying the issue | 8 AM to 5 PM PST (Monday-Friday) | | 1 | Severe impact: Billing issues, login issues | Within one business day of identifying the issue |8 AM to 5 PM PST (Monday-Friday) | | 2 | Medium impact: Core features are unavailable or somewhat slowed; workaround exists | Within two business days of identifying the issue | 8 AM to 5 PM PST (Monday-Friday) | | 3 | Minimal impact: Questions or clarifications regarding features, documentation, or deployments | Within three business days of identifying the issue | 8 AM to 5 PM PST (Monday-Friday) | Cody Free users are not eligible for email Support but can get assistance through our Community Discord. ### Minimum Supported Version for Self-Hosted Instances We support the current major version of the Software and the immediately preceding major version (N minus 1). In situations where a customer is either reluctant or unable to upgrade their product, or if there is no definite timeline for an upgrade, it's important to note that this may significantly impact the level of support we can offer. While we will continue to provide basic support and answer general product questions, our ability to assist in troubleshooting or resolving issues will be limited. This is particularly true for problems that are already addressed in newer versions of our product. Should an issue be definitively resolved by an upgrade, we will inform the customer that upgrading is the necessary course of action. Unfortunately, in such cases, we will be unable to extend further support until the upgrade is completed. In instances where the root cause of a problem is uncertain, but an upgrade could potentially resolve the issue (code host issues and several improvements have been done during the last few releases or we need to bring this issue to Engineering), we will advise to upgrade to N-1 version of our product before continuing to troubleshoot. NOTE: This approach is only for customers that are past the N-1 versions. If within these parameters we may continue to advise to upgrade but will do so with a better understanding of the issue and the probability of it working. ## Sourcegraph cloud SLA (managed instance) >NOTE: Effective: November 14, 2022 — Sourcegraph gives customers a 99.5% uptime commitment on the Business and Enterprise plans. ### Downtime Downtime is the total number of minutes when the Sourcegraph Cloud instance was unavailable during a calendar month. Sourcegraph calculates unavailability using server monitoring software to assess server-side error rates, ping test outcomes, web server evaluations, TCP port examinations, and website tests. It's important to note that, due to the single-tenant architecture of Sourcegraph Cloud, downtime is measured individually for each customer. The following items are not considered as part of downtime: - Slowness or other performance issues specific to individual Sourcegraph features - Issues that are linked to external applications or third-party services, including authentication providers and code hosts - Any products or features labeled as experimental or in beta - External network problems beyond our reasonable control, including connectivity issues involving the client's Internet Service Provider (ISP), Cloudflare, or Google Cloud Platform - Maintenance activities are conducted during Scheduled Downtime. ### Scheduled downtime Scheduled downtime is occasionally required to maintain the proper functioning of your Sourcegraph Cloud instance. In case of such a scheduled downtime, we will provide you with a minimum of 48 hours' advance notice. Please note that the total scheduled downtime will be at most 10 hours in a quarter. ### Uptime commitment Uptime is the percentage of total possible minutes Sourcegraph was available during a calendar month. Our commitment is to maintain at least 99.5% uptime. You can calculate your uptime by using the following formula: ``` [(total minutes in month - Downtime) / total minutes in month] > 99.5% ``` ### Sev 0 - Emergency support scope #### What constitutes a Sev 0 - Emergency? A Severity 0 - Emergency is defined by the following criteria: - The Sourcegraph instance is entirely unavailable or unresponsive for all users. This means the service is inaccessible, and users cannot use any features - Sourcegraph's web user interface displays 4XX or 5XX HTTP error codes on every page. These errors indicate a fundamental problem with the service - All users are unable to log in to the Sourcegraph instance. Authentication is not functioning, preventing user access - A security-related incident poses a significant risk or exposure to the system or data. This may include vulnerabilities, breaches, or other security issues that demand immediate attention to protect the instance and its users #### What is not a Sev 0 - Emergency? A Severity 0 - Emergency typically does not include the following situations: - If only one user is experiencing login issues while most users can still access the system - Performance is slower than usual - Any issues with repository synchronization >NOTE: Custom support agreements apply to Enterprise Plus and Elite customers. Please consult your contract for details regarding custom service-level agreements. ## Sourcegraph Premium Support ### Benefits of Premium Support Purchasing Premium Support provides several of the following services beyond those provided by Sourcegraph's enterprise or enterprise starter support models: - Accelerated Service Level Agreements (SLAs) - Accelerated triaging and queue priority lead to reduced times to resolution. - Elevated Support Expertise - Two named, dedicated senior support engineers possessing deep knowledge and understanding of your specific organization’s setup and tech stack with the ability to assist in the resolution of issues that arise. - Heightened Support via Slack - Faster response times and closer collaboration through direct access to Sourcegraph experts with closer connection to end users and administrators. - More Proactive Collaboration - Hands-on technical guidance to ensure successful migrations, upgrades, and maintenance; early access to new features; quarterly insights reports. ### Premium Support Packages ### Package 1: Premium Support SLAs Premium Support SLAs give customers access to our Support team 24x7 for Severity 0 and 1 issues that critically impact your instance and ability to use Sourcegraph. First response times remain the same according to our SLAs. However, Emergency and Severe Impact issues are supported 24x7 with Premium Support (versus 24x5 without Premium Support). This service is provided for all GA products Sourcegraph offers, not for any [Experimental or Beta features](/admin/beta_and_experimental_features). >NOTE: This package includes access to Slack Support ### Package 2: Dedicated Support Access to named senior support engineers who will have knowledge of your infrastructure and can help reduce the time needed to triage, diagnose, and resolve an issue in your instance. >NOTE: This package includes access to Slack Support ### Package 3: Slack Support Slack Support provides access to creating tickets directly from Slack, allowing customers to resolve tickets directly from a familiar interface and allowing for greater collaboration between Support and customers.
# Sourcegraph Pricing Plan Comparison

This page lists a detailed comparison of the features available in each plan.

| **Features** | **Free** | **Enterprise Starter** | **Enterprise** | | -------------------------------- | ----------------------------------------------------- | ----------------------------------------------------- | ----------------------------------------------------- | | **AI** | | | | | Autocomplete | Unlimited | Unlimited | Unlimited | | Chat messages and prompts | 200/month | Increased limits | Unlimited | | Code context and personalization | Local codebase | Remote codebase (GitHub only) | Remote, enterprise-scale codebases | | Integrated search results | - | ✓ | ✓ | | Prompt Library | ✓ | ✓ | ✓ | | Bring your own LLM Key | - | - | Self-Hosted only | | Auto-edit | - | Beta | Beta | | Aentic chat experience | - | Experimental | Experimental | | **Code Search** | | | | | Code Search | - | ✓ | ✓ | | Code Navigation | - | ✓ | ✓ | | Code Insights | - | - | ✓ | | Code Monitoring | - | - | ✓ | | Batch Changes | - | - | ✓ | | **Deployment** | | | | | Cloud deployment | Multi-tenant | Multi-tenant | Single tenant | | Self hosted option | - | - | ✓ | | Private workspace | - | ✓ | ✓ | | **Admin and Security** | | | | | SSO/SAML | Basic (GH/GL/Google) | Basic (GH/GL/Google) | ✓ | | Role-based access control | - | - | ✓ | | Analytics | - | Basic | ✓ | | Audit logs | - | - | ✓ | | Guardrails | - | - | Beta | | Indexed code | - | Private | Private | | Context Filters | - | - | ✓ | | **Compatibility** | | | | | Code hosts | Local codebase | GitHub | All major codehosts | | IDEs | VS Code, JetBrains IDEs, Visual Studio (Experimental) | VS Code, JetBrains IDEs, Visual Studio (Experimental) | VS Code, JetBrains IDEs, Visual Studio (Experimental) | | Human languages | Many human languages, dependent on the LLM used | Many human languages, dependent on the LLM used | Many human languages, dependent on the LLM used | | Programming languages | All popular programming languages | All popular programming languages | All popular programming languages | | **Support** | | | | | Support level | Community support | Community support | Enterprise support | | Dedicated TA support | - | - | Add-on | | Premium support | - | - | Add-on |
# FAQs

Learn about some of the most commonly asked questions about Sourcegraph.

## What's the difference between Free, Enterprise Starter, and Enterprise plans? Free is best for individuals working on hobby projects. Enterprise Starter is for growing organizations who want Sourcegraph's AI & search experience hosted on our cloud. Enterprise is for organizations that want AI and search across the SDLC with enterprise-level security, scalability, and flexible deployment. ## How are autocompletions counted for the Cody Free plan? Cody autocompletions are counted based on the number of suggestions served to the user in their IDE as ghost text. This includes all suggestions on whether or not the user accepts them. ## How does Sourcegraph's context and personalization work? Cody can retrieve codebase context to personalize responses in several ways. For Free and Pro users, context is retrieved from their local repositories. The Enterprise Starter and Enterprise plans use Sourcegraph's search backend to retrieve context. This method pulls context from a team's full codebase at any scale. ## What forms of support are available for paid plans? Email and web portal support is available to both Enterprise Starter and Enterprise customers, and you can [read more about our SLAs](/sla). Premium support with enhanced SLAs is also available as an add-on for Enterprise customers. ## Can I upgrade or downgrade my plan? Users can upgrade or downgrade between Free and Pro within their account settings anytime. For Pro users, upgrading to Enterprise Starter is also possible, but doing so currently does not cancel your subscription, and you must cancel it yourself. To upgrade to Enterprise, please [contact our Sales team](https://sourcegraph.com/contact/request-info). ## What's the difference between "flexible LLM options" and "bring your own LLM key"? Flexible LLM options: Users can select from multiple options to use for chat. Bring your own LLM key: Enterprise customers can optionally provide their own LLM API key for supported LLMs (including for LLM services such as Azure OpenAI and Amazon Bedrock). In this scenario, customers pay for their own LLM consumption, and we will provide a pricing discount with your plan. ## Does Sourcegraph use my code to improve the models used by other people? For Enterprise and Enterprise Starter customers, Sourcegraph will not train on your company's data unless your instance admin enables fine-tuning, which would customize an existing model exclusively for your use. For Free and Pro users, Sourcegraph may use your data to fine-tune the model you are accessing unless you disable this feature. ## Can Sourcegraph be run fully self-hosted? Sourcegraph requires cloud-based services to power its AI features. For customers looking for a fully self-hosted or air-gapped solution, please [contact us](https://sourcegraph.com/contact/request-info). ## Is an annual contract required for any of the plans? Pro and Enterprise Starter plans are billed monthly and can be paid with a credit card. ## How are active users counted and billed for Cody? This only applies to Cody Enterprise contracts. Cody Pro and Enterprise Starter users pay for a seat every month, regardless of usage. ‍A billable user is one who is signed in to their Enterprise account and actively interacts with the product (e.g., they see suggested autocompletions, run commands or chat with Cody, start new discussions, clear chat history, or copy text from chats, change settings, and more). Simply having Cody installed is not enough to be considered a billable user. ## How are active users calculated for Sourcegraph Code Search and Code Intelligence Platform? A billable active Code Search or Code Intelligence Platform user is signed into and accesses Sourcegraph. This includes but is not limited to: - Visiting the Sourcegraph web application or using a browser extension, editor extension, or other client while signed in - Conducting a search in the Sourcegraph UI or extension - Hovering and navigating code in the Sourcegraph UI or extension - Viewing a file or repository in Sourcegraph - Creating, viewing, or receiving a code monitor - Creating, viewing, modifying, or applying a batch change - Creating, viewing, or modifying a code insight - Creating, viewing, or modifying a search notebook ## How is this measured in the product? A user who has accessed a Sourcegraph product is counted as active once they complete an action that represents usage reflected above. User counts and usage metrics are in [Sourcegraph Analytics](../analytics). ## Is my data secure when connected to Code Search? Sourcegraph has security and reliability controls built for the most demanding enterprises. To learn more, see our [Security page](https://sourcegraph.netlify.app/security). ## What if I want AI or Code Search Enterprise only? You can purchase Enterprise plans for AI or Code Search only. [Contact us](https://sourcegraph.com/contact/request-info) to learn more. ## Which code hosts is the Enterprise Starter plan compatible with? The Enterprise Starter plan is currently compatible with GitHub. Its limit for indexing is 100 repositories for search and context. ## What are the limits of the Enterprise starter plan? The Enterprise Starter plan supports up to 50 developers and, alongside a limit of 100 repositories for search and context, also includes 5Gb of storage. Adding additional seats gives you 1GB of additional storage per seat, for a maximum total of 10GB. ## Billing FAQs for Enterprise Starter ## How do I cancel subscription renewal? In the **Workspace settings > Billing** page, you can cancel the subscription and continue having access to your workspace until the end of your current billing period (that is indicated in the UI). ## How do I cancel subscription and delete my workspace immediately? On the **Workspace settings > General settings** page, you can delete your workspace. This will immediately remove access and cancel your subscription. ## How is the subscription renewal dates determined? Your subscription renewals are scheduled to happen on the same day of the month. On shorter months (e.g., day 31 on April, which only has 30 days), the last day of the month will be the subscription renewal day instead. ## How do I access my invoices? You can access your invoices via the **Workspace settings > Billing** page by clicking the **View invoices** button, which takes you to the Stripe Customer Portal. Note that invoices are not emailed every month. ## How do I pay my invoice if my subscription is past due? After updating or resolving your payment method issue that occurred during the automatic subscription renewal, you may do one of the following to pay the invoice for your past-due subscription: 1. Wait for our system to re-attempt the charge for the invoice. It usually takes up to 24 hours. If it does not happen after 24 hours, please contact [Support](support@sourcegraph.com) to resolve the issue. 1. In the **Workspace settings > Billing** page, click the **View invoices** button, which takes you to the Stripe Customer Portal. Then, pay the invoice there. Our system will reconcile your payment within 24 hours. If it does not reconcile after 24 hours, please contact [Support](support@sourcegraph.com) to resolve the issue. 1. Contact [Support](support@sourcegraph.com) to request re-attempt the charge for the invoice using the payment method on file. ## Are there any refunds for the subscription? We don't offer refunds, but if you have any queries regarding the Enterprise Starter, please write to support@sourcegraph.com, and we'll help resolve the issue.
# Sourcegraph Pricing Plans

Sourcegraph offers three different pricing plans based on your needs.

# Free

Learn about the Sourcegraph's Free plan and the features included.

Sourcegraph's Free plan is designed for hobbyists, and light usage is aimed at users with personal projects and small-scale applications. It offers an AI editor assistant with a generous set of features for individual users, like autocompletion and multiple LLM choices for chat. ## Features The Free plan includes the following features: | **AI features** | **Compatibility** | **Deployment** | **Admin/Security** | **Support** | | ----------------------------------------------------------------------------- | --------------------------------------------------- | ------------------ | ------------------------------------------ | ---------------------- | | Reasonable use autocomplete limits | VS Code, JetBrains IDEs, and Visual Studio | Multi-tenant Cloud | SSO/SAML with basic GitHub, GitLab, Google | Community support only | | Reasonable use chat messages and prompts per month | All popular coding languages | - | - | - | | Multiple LLM selection (Claude 3.5 Sonnet, Gemini 1.5 Pro and Flash) | Natural language search | - | - | - | ## Pricing and billing cycle There is no billing cycle, as it's free to use and supports one user per account. If you exceed your daily limits, you will have to wait until the end of the month to use the feature again. You can upgrade to our Enterprise Starter plan for more advanced features and usage limits. ## Free vs. Enterprise Starter comparison The Enterprise Starter plan provides extended usage limits and advanced features compared to the Free plan. Here's a side-by-side comparison of the two: | **Feature** | **Free** | **Enterprise Starter** | | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | | **Description** | - AI editor assistant for hobbyists or light usage | - AI and search for growing organizations hosted on our cloud | | **Price** | - $0/month
- 1 user | - $19/user/month
- Up to 50 devs | | **AI features** | - Autocompletions
- 200 chat messages and prompts per month
- Multiple LLM choices for chat | - Code autocomplete and chat
- More powerful LLMs for chat (GPT-4o, Claude 3 Opus)
- Integrated search results | | **Code Search features** | N/A | - Code Search
- Symbol Search | | **Deployment types** | - Multi-tenant Coud | - Multi-tenant Cloud
- Private Workspace
- Privately indexed code (100 repos) | | **Compatibility** | - VS Code, JetBrains IDEs, and Visual Studio
- All popular coding languages
Natural language search
- All major code hosts | - VS Code, JetBrains IDEs, and Visual Studio
- All popular coding languages
Natural language search
- Code hosted on GitHub | | **Support** | - Community support only | - 9x5 Support | ## Moving to Enterprise Starter plan From your Sourcegraph dashboard, you can upgrade to the Enterprise Starter by clicking the **Create workspace for my team** button. Enter your workspace name and URL, and choose the number of seats. You can start with a single seat and add more later from the **Users** settings section. Click the **Create workspace** button to navigate to the payment page. Here, you can view your subscription details and enter your payment details for a successful transaction.
# Enterprise

Learn about the Sourcegraph's Enterprise plan and the features included.

Sourcegraph offers multiple Enterprise plan options, including Enterprise Dedicated Cloud (default) and Enterprise Self Hosted (on-request) for organizations and enterprises that need AI and search with enterprise-level security, scalability, and flexibility. ## Features breakdown Here's a detailed breakdown of features included in the different Enterprise plan options. | **Feature** | **Enterprise Dedicated Cloud** | **Enterprise Self Hosted** | | ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | **Description** | - AI and search with enterprise-level security, scalability, and flexibility | - AI and search with enterprise-level security, scalability, and flexibility | | **Price** | - $59/user/month
- 25+ devs | - $59/user/month
- Contact Sales | | **AI features** | - Everything in Enterprise Starter | - Everything in Enterprise Starter
- Bring your own LLM key | | **Code Search features** | - Everything in Enterprise Starter, plus:
- Batch Changes
- Code Insights
- Code Navigation | - Everything in Enterprise Starter, plus:
- Batch Changes
- Code Insights
- Code Navigation | | **Deployment types** | - Single-tenant Coud | - Self- Hosted | | **Compatibility** | - Everything in Enterprise Starter, plus:
- Enterprise admin and security features
- All major code hosts
- Guardrails
- Context Filters | - Everything in Enterprise Starter, plus:
- Enterprise admin and security features
- All major code hosts
- Guardrails
- Context Filters | | **Support** | - 24x5 support with options like:
- TA support
- Premium Support Offerings
- Forward Deployed Engineer (FDE) | - Enterprise support with options like:
- Dedicated TA support
- Premium Support Offerings
- Forward Deployed Engineer (FDE) |
# Enterprise Starter

Learn about the Enterprise Starter plan tailored for individuals and teams wanting private code indexing and search to leverage the Sourcegraph platform better.

The Enterprise Starter plan offers a multi-tenant Sourcegraph instance designed for individuals and teams. It provides the core features of a traditional Sourcegraph instance but with a simplified management experience. This plan provides a fully managed version of Sourcegraph (AI + code search with integrated search results, with privately indexed code) through a self-serve flow. ## Team seats The Enterprise Starter plan is priced at **$19 per month per seat**. You can add or remove team members at any time. Existing Cody Pro users can also sign up for the Enterprise Starter by paying $19 per seat. However, their Cody Pro subscription will neither be upgraded nor canceled. Instead, they will have two live subscriptions. ## Enterprise Starter team roles The Enterprise Starter plan includes the following team-level roles: - **Admin**: Has full access to the workspace, including the ability to manage repos, users, billing, and settings - **Member**: Can access repositories in the workspace and use the supported features ## Billing Workspaces on the Enterprise Starter plan are billed monthly based on the number of team seats purchased. In case of overdue or failing payment, there is a grace period during which the workspace admins will receive a daily notification to complete the transaction. If you fail to make the payment after the grace period, your workspace will be deleted, and you will not be able to recover your data. Please also see [FAQs](faqs.mdx) for more FAQs, including how to downgrade Enterprise Starter. ## Features supported The Enterprise Starter plan supports a variety of AI and search-based features like: | **AI features** | **Code Search** | **Management** | **Support** | | -------------------------------------- | ------------------------------ | --------------------------------------------------------- | ------------------------- | | Code autocompletions and chat messages | Indexed Code Search | Simplified admin experience with UI-based repo-management | Support with limited SLAs | | Powerful LLM models for chat | Indexed Symbol Search | User management | - | | Integrated search results | Searched based code-navigation | GitHub code host integration | - | | Cody integration | - | - | - | ## Limits Sourcegraph Enterprise Starter offers the following limits: - Max 50 users per workspace - Max 100 repos per workspace - Starts with 5 GB of storage - 1 GB storage per seat added - 10 GB max total storage ## Workspace settings After creating a new workspace, you can switch views between your personal and workspace accounts. You can configure different **Workspace settings**. These include options for: - **General Settings**: Helps you configure how your workspace is described or accessed with options like workspace name, URL, and deleting the workspace - **Users**: Manage permissions, assign seats, or invite new users - **Billing**: Manage your monthly billing cycle for all your purchased seats - **Repository Management**: Add, remove, and view the status of your connected repositories - **User settings**: Navigates you to your personal account's settings ![workspace-settings](https://storage.googleapis.com/sourcegraph-assets/Docs/workspace-settings-125.jpg) ## Getting started with workspace A workspace admin can invite new members to their workspace using their team member's email address. Once the team member accepts the invitation, they will be added to the workspace and assigned the member role. Next, the member is asked to connect and authorize the code host (GitHub) to access the private repositories indexed in your Sourcegraph account. If you skip this step, the member won't be able to access any of the private repositories they have access to. However, they can still use the public search via the Sourcegraph code search bar. ![member-authorization](https://storage.googleapis.com/sourcegraph-assets/Docs/member-authorization.jpg) ## Repository Management From the Repository Management settings, workspace admins can configure various settings for connecting code hosts and indexing repositories in their workspace. You can index up to 100 repos per workspace. ![repo-management](https://storage.googleapis.com/sourcegraph-assets/Docs/repo-management.jpg) From here, you can: - Use the public code search to add and index open source repos in your workspace - Add multiple organizations to index private repos When you add a new organization, you must authorize access and permission for all repositories or selected ones. To index a repository from your organization, - Click and select it from the repository list - Next, from the search bar, type the repo name you are looking for - Click it to add the repository to your workspace - The status of the repos will change to **TO BE ADDED** in the right sidebar with a **Save Changes** button - Next, the repo gets a **QUEUED** status, and it takes some time to process - Finally, it gets indexed with a **CLONED** status As you add more repos, you get logs for the number of repos added, storage used, and their status. To remove any repo from your workspace, click the repo name that changes the repo status **TO BE REMOVED**. Click the **Save Changes** button to confirm it. ![repo-list](https://storage.googleapis.com/sourcegraph-assets/Docs/repo-list.jpg)
# Code ownership This feature is in beta. Code ownership is aimed at helping find the right person and team to contact, for any question, at any time. We are starting out with code ownership, ownership inference and assignments and are exploring ways to help you find someone to answer _every_ question. ## Enabling code ownership Code ownership is enabled by default. If you like to disable it from being shown in the UI, you can create the feature flag `enable-ownership-panels` and set it to `false`: - Go to **Site-admin > Feature flags** - If the feature flag `enable-ownership-panels` doesn't yet exist, click **Create feature flag** - Under **Name**, put `enable-ownership-panels` - For **Type** select **Boolean** - And set **Value** to **False** - Click **Create flag** ## Concepts **Owner**: An owner is defined as a person or a team in Sourcegraph. A _person_ can be: - a Sourcegraph user which we were able to resolve from the `CODEOWNERS` handle or email, in which case we link to their profile. - an unknown user for which we were unable to resolve a profile, in which case we will return the `CODEOWNERS` data we have. A _team_ is a group of Sourcegraph users represented by a common handle, which is a new feature that we added. [Read more about how to manage teams in Sourcegraph](/admin/teams/). ## Code ownership Code ownership is set in 2 different ways: - [The `CODEOWNERS` format](/own/codeowners_format) - [Assigned ownership](/own/assigned_ownership) ## Limitations - Code ownership support has been released as an MVP for 5.0. In the future of the product we intend to infer ownership beyond `CODEOWNERS` data. - The feature has not been fully validated to work well on large repositories or large `CODEOWNERS` rulesets. This is a future area of improvement, but please contact us if you run into issues. ## Browsing ownership The ownership information is available for browsing once ownership data is available through [a `CODEOWNERS` file](#code-ownership). When displaying a source file, there is a bar above the file contents. - On the left-hand side, it displays the most recent change to the file. - On the right-hand side it displays the code ownership bar with at most 2 file owners. Any additional number of owners is also displayed. ![File view showing code ownership bar on the right hand side above the file contents](https://storage.googleapis.com/sourcegraph-assets/docs/own/blob-view.png) After clicking on the code ownership bar, a bottom panel appears listing all the owners. ![File view with the ownership tab selected in the bottom panel](https://storage.googleapis.com/sourcegraph-assets/docs/own/blob-view-panel.png) There is always a single rule in a `CODEOWNERS` file that determines ownership (if any). Each owner listed in the bottom panel has a description found by clicking the collapsible arrow: _Owner is associated with a rule in a `CODEOWNERS` file_. Clicking this description links to the line containing the responsible rule in the `CODEOWNERS` file. If any email information has been found for the owner, clicking the mail icon will start an email to them. ## Ownership search ### Searching for files with owners Code ownership is a first-class citizen in search. Ownership can be either a query input or a search result: - `file:has.owner(user@example.com)` keeps only the search results associated with given user (here referred to by e-mail). - `-file:has.owner(@username)` removes all results owned by specific user (here referred to by name). Ownership predicate can also be used without parameters: -`file:has.owner()` will only include files with an owner assigned to them. -`-file:has.owner()` will only include files without an owner. When performing a search the `select:file.owners` predicate will return the owners for the result of that search. For instance one can find all the owners of TypeScript files in a given repository by using `repo:^github\.com/sourcegraph/sourcegraph$ lang:TypeScript select:file.owners`. ### Find commits in given release for given owner To find all commits between versions `5.0` and `5.1` made by `sourcegraph/own` team, the following query could be used: `repo:^github\.com/sourcegraph/sourcegraph$@5.1:^5.0 type:commit file:has.owner(sourcegraph/own)` Same query can be run for any owner (a person or a team). ## Troubleshooting ### Memory issues and worker crashes The Own background processes (such as ownership signals computation) can consume significant memory, potentially causing worker jobs to crash due to excessive memory usage. If you experience memory problems or worker crashes related to Own: 1. **Disable ownership signals**: Go to **Site admin > Code graph > Ownership signals** and disable the signals that are causing issues: - Recent contributors signal - Recent views signal - Analytics computation 2. **Exclude large repositories**: Use the repository filtering options to exclude large repositories from ownership signal computation. 3. **Monitor resource usage**: Check your instance's memory usage after disabling signals to confirm the issue is resolved. If memory issues persist after disabling ownership features, consider whether Own functionality is essential for your use case. ## Further reading In order to learn more please check out our references: - [CODEOWNERS format](/own/codeowners_format) - Guide to using the CODEOWNERS file format to define ownership - [Configuration](/own/configuration_reference) - Full list of ownership configuration options # Configuration reference ## Ownership signals and background compute Ownership signals are used to guide the assignment of ownership. These are: * **Recent contributors signal** counts files modified by commits in the last 90 days. * **Recent views signal** counts file views within Sourcegraph in the last 90 days. Both of these signals are computed by background tasks. The values of signals are aggregated and bubble up the file tree. That is, for the Ownership data displayed `/a/` directory, all descendant file signals contribute. For instance contributions and views of `/a/b/c.go`. The **Site admin > Code graph > Ownership signals** page allows enabling and disabling each signal individually. These need to be explicitly enabled by site admin in order for signals to surface in the UI. ![Site admin ownership configuration page](https://sourcegraphstatic.com/own-signals-configuration-dark.png) ### Repository filtering In some cases ownership signals need to be disabled for specified repositories: * Ownership signals are not desired for certain repositories. * Computing signals is expensive due to size of some repositories. **Exclude repositories** section under each signal in **Site admin > Code graph > Ownership signals** allows to match repositories to exclude from computing ownership signals. Materialized list of excluded repositories is displayed for feedback. ![Site admin ownership configuration page](https://sourcegraphstatic.com/own-signals-exclude-dark.png) ## Analytics In order to measure how many files have owners, Sourcegraph exposes analytics through **Site admin > Analytics > Own**. These present percentage of files that have: * Any ownership associated, * assigned ownership (through the UI), * ownership via matching rule in CODEOWNERS file. Analytics data is computed periodically. The background process for computing analytics data has to be enabled explicitly through **Site admin > Code graph > Ownership signals**. This is because the process can become computationally expensive. ## Assigned ownership access control In order to grant users the ability to assign ownership, please use [ownership permission](/admin/access_control/ownership) in role-based access control. This is a coarse-grained permission, allowing users to assign ownership throughout the instance. At this point there is no finer-grained ownership assigning access control. ## Disabling ownership in the UI Ownership data is displayed in various places in Code search user interface, among others: * in a card on repository and directory pages, * in a top bar on file page. If needed, the visibility of ownership data embedded in Code search UI can be disabled by creating a boolean feature flag `enable-ownership-panels` and setting its value to `false`. # Codeowners ingestion This feature is in beta. Code ownership allows you to surface ownership data using `CODEOWNERS` files. This is done automatically if there is a committed `CODEOWNERS` file in your repository at any of the following locations: ```md CODEOWNERS .github/CODEOWNERS .gitlab/CODEOWNERS docs/CODEOWNERS ``` However, it might be you do not want to have a `CODEOWNERS` file committed to your repository (for example, to avoid automatic review requests), or you would like to overwrite the existing one. Sourcegraph provides a UI and CLI to ingest a `CODEOWNERS` file per-repository, that overrides any existing committed file. You can ingest one `CODEOWNERS` file per repository. At this time the same ingested `CODEOWNERS` file applies to all revisions. ## Ingesting a file through the UI Navigating to any repository page, clicking the Ownership button will surface information about any ingested `CODEOWNERS` file, and will allow you to upload or update an existing one. ![Codeowners ingestion UI on sourcegraph/sourcegraph](https://storage.googleapis.com/sourcegraph-assets/docs/images/own/codeowners_ingestion_ui.png) ## Ingesting a file with src-cli There is the option to ingest data with the Sourcegraph [src-cli](/cli/quickstart). The CLI provides `add`, `update`, `delete`, and `list` functionality. ```bash 'src codeowners' is a tool that manages ingested code ownership data in a Sourcegraph instance. Usage: src codeowners command [command options] The commands are: get returns the codeowners file for a repository, if exists create create a codeowners file update update a codeowners file delete delete a codeowners file Use "src codeowners [command] -h" for more information about a command. ``` The input file can be written inline or passed in. ## Limitations - Uploaded `CODEOWNERS` files must use either Sourcegraph usernames or email addresses for correct user matching to occur. `CODEOWNERS` files committed to the repo should use either usernames of the codehost the repo is on (e.g. GitHub) or email addresses. - The file should respect `CODEOWNERS` formatting for code ownership to surface useful information. No formatting validation is done at upload time. - Only site admins can add, update or delete a `CODEOWNERS` file through the ingestion API. - Ingested `CODEOWNERS` files are limited to a size of 10Mb if uploaded through the client. # The CODEOWNERS format Code ownership is defined as a strict ruleset. Files can be assigned to owners. To define rulesets for code ownership, we make use of the `CODEOWNERS` format. `CODEOWNERS` files contain a sequence of matching rules - a glob pattern and zero or more owners. **A repository has at most one `CODEOWNERS` file.** ## Specifying Owner information Owners can be defined by a username/team name or an email address. Using email addresses is generally recommended, as email addresses are most likely the same across different platforms, and are independent of a user having registered yet. In Sourcegraph, a user can add multiple email addresses to their profile. All of those would match to the same user. For committed `CODEOWNERS` files, the usernames are usually the username **on the code host**, so they don't necessarily match with the Sourcegraph username. This is a known limitation, and in the future, we will provide ways to map external code host names to Sourcegraph users. For now, you can search for a user by their code host username, or switch to using emails in the `CODEOWNERS` files, which will work across both Sourcegraph and the code host. ## File format The following snippet shows an example of a valid `CODEOWNERS` file. ``` *.txt @text-team # this is a comment explaining why Alice owns this /build/logs/ alice@sourcegraph.com /cmd/**/test @qa-team @user ``` - Asterisk `*` is a wildcard that matches N tokens in a path segment. **Example**: `doc/*/own` will match `doc/ref/own` and `doc/tutorial/own`, but not `doc/a/b/own` - Double `**` asterisk matches any sub-path. **Example**: `doc/**/own` will match `doc/ref/own` and `doc/a/b/own` - Starting a pattern with `/` anchors matches at the repository root. **Example**: `/docs/*` matches `/docs/a` and `/docs/b` but not `/src/docs/a`. - Trailing slash `/` matches any file within the directory tree (so it is equivalent to trailing `/**`). **Example**: `docs/` matches `/testing/docs/foo` and `/docs/foo/bar`, but does not match `/docs` or `/testing/docs`. The rules are considered independently and in order. Rules farther down the file take precedence. Only **one** rule matches. So for instance for `/build/logs/log-1.txt` the owner will only be `alice@sourcegraph.com` and not `@text-team` since the `/build/logs/` rule will take precedence over `*.txt` rule. ## Limitations - GitLab allows sections in `CODEOWNERS` files, these are not yet supported and section markers are ignored - [Code Owners for Bitbucket](https://marketplace.atlassian.com/apps/1218598/code-owners-for-bitbucket?tab=overview&hosting=cloud) inline defined groups are not yet supported To configure ownership in Sourcegraph, you have two options: ## Committing a `CODEOWNERS` file to your repositories > Use this approach if you prefer versioned ownership data. You can simply commit a `CODEOWNERS` file at any of the following locations for it to be picked up automatically by code ownership: ``` CODEOWNERS .github/CODEOWNERS .gitlab/CODEOWNERS docs/CODEOWNERS ``` Searches at specific commits will return any `CODEOWNERS` data that exists at that specific commit. ## Uploading a `CODEOWNERS` file to Sourcegraph > Use this approach if you don't want to commit `CODEOWNERS` files to your repos, or if you have an existing system that tracks ownership data and want to sync that data with Sourcegraph. Read more on how to [manually ingest `CODEOWNERS` data](/own/codeowners_ingestion) into your Sourcegraph instance. The [docs](/own/codeowners_ingestion) detail how to use the UI or `src-cli` to upload `CODEOWNERS` files to Sourcegraph. # Assigned ownership Owners (both users and teams) can be manually assigned. Assignment can be done on a repository, directory or a file level. Assigned ownership propagates in a top-down manner: e.g. - If an owner is assigned to the repository, they will be considered an owner for all files and directories of this repository. - If an owner is assigned to some directory (e.g. `repo-name/src/abc`) within the repo, they will be considered an owner for all the directories and files within `repo-name/src/abc`, but not for any other directories and files higher up in the hierarchy. - If an owner is assigned to some file (e.g. `repo-name/src/abc/a.go`) within the repo, they will be considered an owner only for this file. ## Who can assign ownership Only site admins can assign ownership by default. For other users, [RBAC should be used](/admin/access_control/ownership) to grant assign ownership right. [More](/admin/access_control) about RBAC in Sourcegraph. ## How to assign ownership ### Repository level ownership Go to the repository page and click "Ownership" button. ![Repository page with Ownership button selected](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-ownership-1-dark.png) 1. Click "Add owner" button, the modal window will open. 2. Activate "New owner" panel. 3. Search for the user/team to assign ownership to. 4. Click "Add owner" button on the modal window. ![Add repository owner](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-2-dark.png) Owner is assigned successfully. ![Repository owner added](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-3-dark.png) ### Directory level ownership Go to any directory view (in our example it is `cmd/gitserver/internal`) and click "Show more" on Own panel. ![Repository page with Ownership button selected](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-dir-1-dark.png) 1. Click "Add owner" button, the modal window will open. 2. Activate "New owner" panel. 3. Search for the user/team to assign ownership to. 4. Click "Add owner" button on the modal window. ![Add repository owner](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-2-dark.png) Owner is assigned successfully. ![Repository owner added](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-3-dark.png) ### File level ownership Go to the blob view of any file (in our example it is `cmd/gitserver/internal/cleanup.go`). 1. Click on the Own bar in the top-right corner. 2. Click "Add owner" button in the bottom-right corner of an opened Ownership tab. 3. Activate "New owner" panel. 4. Search for the user/team to assign ownership to. 5. Click "Add owner" button on the modal window. ![Add repository owner](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-dir-2-dark.png) Owner is assigned successfully. ![Repository owner added](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-dir-3-dark.png) ## How to remove an assigned owner ### Repository and directory level Go to the repository page and click "Ownership" button **or** Go to any directory view and click "Show more" on Own panel. Click on "Remove ownership" button. ![Repository/directory owner removed](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-remove-repo-1-dark.png) ### File level Go to the blob view of any file and to the Ownership tab. Click on "Remove ownership" button. ![File owner removed](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-remove-file-1-dark.png) ### Note on removing assigned owners Owners can be removed only at the same level they were initially assigned. For example: - If an owner is assigned to the repository, they should be removed from the repository level. They cannot be removed from any directory/file within this repo, even though they are considered owners of these directories/files. - If an owner is assigned to some directory within the repo, they should be removed from the same directory. They cannot be removed from any subdirectory/file below this directory, even though they are considered owners of these subdirectories/files. - If an owner is assigned to some file within the repo, they should be removed from the same file. **"Remove ownership" buttons are disabled and the tooltip is shown when a user is attempting to remove an owner from the different place from where this owner has been assigned.** ![Remove ownership disabled tooltip](https://storage.googleapis.com/sourcegraph-assets/docs/own/assigned-owners-cannot-remove-dark.png) # Quickstart for Notebooks In this guide, you'll create your first Notebook ## The Notebooks interface The Notebooks interface will be familiar if you've ever used Jupyter or iPython notebooks before. ![A new notebook](https://storage.googleapis.com/sourcegraph-assets/docs/images/notebooks/notebooks_ui_overview) ### Run all blocks The Run all blocks button allows you to render all the blocks in the Notebook with one click. You'll want to get in the habit of using this button every time you start reading a Notebook. ![The Run all blocks button](https://storage.googleapis.com/sourcegraph-assets/docs/images/notebooks/notebooks_run_all.jpeg) ### Autosave Notebooks are automatically saved so you'll never lose your work. ![null](https://storage.googleapis.com/sourcegraph-assets/docs/images/notebooks/notebooks_autosave.jpeg) ### Supported block types Like Jupyter notebooks, Sourcegraph Notebooks support Markdown. They also support two special block types: Query and Code. #### Query blocks Query blocks allow you to enter any valid Sourcegraph query and view the results right inside the notebook. The query block input supports the same typeahead functionality as the main Sourcegraph search bar. ![](https://storage.googleapis.com/sourcegraph-assets/docs/images/notebooks/notebooks_query_block) #### Markdown blocks Markdown blocks support all the Markdown features you'd expect, including rendering images and syntax highlighting. #### Code blocks Code blocks are another special block type that allow you show a file range from any file on Sourcegraph. You can use the interface to fill in the range you want, or just select a code block and paste in a valid Sourcegraph URL to have the inputs auto-filled. ![](https://storage.googleapis.com/sourcegraph-assets/docs/images/notebooks/notebooks_code_block.mp4) # Create a new Notebook - Navigate to [https://sourcegraph.com/notebooks](https://sourcegraph.com/notebooks) and click "Create notebook". - Click on the name to edit and rename the Notebook "React components" - Create a Markdown block and enter the text below. ``` # React class components React class components are no longer the preferred style of component creation. Today, functional components are the standard. Below are some examples of class components in the Sourcegraph codebase. ``` - Click the render button, or press Cmd+Return to render the block - Next, create a query block and enter the following query to find some examples of class components in the Sourcegraph codebase. ``` repo:^github\.com/sourcegraph/sourcegraph$ lang:TypeScript extends React ``` - Render the block with Cmd+Return - Next, let's find some recent commits that include class components. - Click the "duplicate" button to duplicate the block and modify the query to return commits that added our search phrase. ``` repo:^github\.com/sourcegraph/sourcegraph$ lang:TypeScript extends React type:diff select:commit.diff.added ``` - Your Notebook should now look like [this](https://sourcegraph.com/notebooks/Tm90ZWJvb2s6MTE4). - **Optional:** share you notebook by clicking "Private" at the top right of the screen and select "Public" to share your Notebook with the community! # Sharing Notebooks Notebooks support the following sharing scheme. ## User namespace ### Private Notebooks This is the default permissions level for all new notebook. Only the creator can view and edit the notebook. ## Sourcegraph organization namespace Find out more about Sourcegraph organizations and how to create and configure them on the [organizations docs page](/admin/organizations). ### Private organization Notebooks Only organization members can view and edit the notebook. ### Public organization Notebooks In self-hosted and managed Sourcegraph instances, everyone who has access to the instance can view the notebook. On [Sourcegraph.com](https://sourcegraph.com), anyone can view the Notebook. In both cases, only members of the owning Sourcegraph organization can edit the Notebook. ![](https://storage.googleapis.com/sourcegraph-assets/docs/images/notebooks/notebook_sharing.gif) # Embedding Notebooks A notebook can be embedded using a standard iframe element. In order for the iframe to load, the user must be logged into Sourcegraph. The notebook embedding URL is an authenticated endpoint. ## Domain considerations Certain browsers (i.e. Safari and Firefox) block cross-domain cookies from being sent in iframe requests. This will prevent an embedded notebook from being displayed, even when a user is logged into Sourcegraph from the same browser. To ensure that notebook embedding requests will be permitted to load for all of your logged-in Sourcegraph users, the Sourcegraph instance must be hosted on the same domain as the page that loads the iframe element. For Cloud customers, see [Custom Domains](/cloud/#custom-domains). ## How to embed To create the embedding URL, copy the notebook URL (e.g. `https://your-sourcegraph-instance.com/notebooks/notebook-id`), and add the `/embed` prefix directly before the `/notebooks` segment: ``` https://{your-sourcegraph-instance.com}/embed/notebooks/{notebook-id} ``` Once you have the embedding URL, create an iframe element and use the embedding URL as the `src` attribute value. See example iframe below: ```html ``` ## Security We recommend using the `sandbox` attribute to apply extra security restrictions to the iframe. Notebooks require three exceptions: `allow-scripts` allows executing Javascript scripts, `allow-same-origin` allows access to local storage and cookies, and `allow-popups` allows opening links in a separate tab. ## Enable embedding notebooks on private instances Embedding is disabled by default on private instances. A site-admin can enable embedding by running the following GraphQL mutation in the API console, located at `https://{your-sourcegraph-instance.com}/api/console`: ```graphql mutation { createFeatureFlag(name: "enable-embed-route", value: true) { ... on FeatureFlagBoolean { name value } } } ``` # Notebooks Notebooks enable powerful live–and persistent–documentation, shareable with your organization or the world. As of release 3.39, Notebooks are Generally Available (GA). If you're a Sourcegraph enterprise user and are on version 3.39 or later, you'll find Notebooks in the header navigation, or by going to `https:///notebooks`. Inspired by Jupyter Notebooks and powered by Markdown and Sourcegraph's code search, Notebooks let you and your team create living documentation that interacts directly with your code. You can leverage Notebooks to onboard a new teammate, document known vulnerabilities, a common pattern in your codebase, or useful Sourcegraph queries. ![](https://storage.googleapis.com/sourcegraph-assets/docs/images/notebooks/notebooks_home.gif) Notebooks have powerful content creation features, like multiple block types, each with their own unique capabilities. If you're familiar with Jupyter Notebooks, then you already understand the blocks concept. You can add as many of each block as you want to a Sourcegraph notebook. ## Notebook types Notebooks can be created in two ways. Through the web interface or via special Markdown files with the special `.snb` extension. To view file-based notebooks you must view the files in the file view on sourcegraph.com or on your Sourcegraph instance. ### Web-based notebooks The simplest way to get started with Notebooks is to create one using the web interface. Notebooks created this way have the advantage of being interactive, letting you see the content of your blocks in realtime as you create your notebook. You can also create web-based notebooks by importing plain Markdown files and then augmenting them with Sourcegraph notebook block types in the web interface. A new notebook will automatically be created when you import a standard markdown file. From there, you can modify it however you like in the web interface. Web-based notebooks are automatically saved as they're edited. There is currently no version control, version history, or versioning system. ### File-based notebooks Alternatively, you can create notebooks using text files with the `.snb` file extension. These files are rendered specially by Sourcegraph (either on sourcegraph.com or within your Sourcegraph instance) to display notebook blocks alongside standard Markdown blocks. Whenever you view a `.snb` file on Sourcegraph, you'll see a "Run all blocks" button near the top of the notebook, which will execute all the notebook blocks at once. Markdown and file blocks are rendered by default. File-based notebooks have the advantage of living anywhere you store text files. The disadvantage comes during composition, as you won't be able to see the contents of your blocks while you create your notebook. ### Combined approaches #### Compose online and export to disk If you prefer to keep your notebooks in your repos but want to compose them on the web, you can get the best of both worlds by composing your notebooks on your sourcegraph instance and then exporting them to your repositories on disk. #### Embed notebooks anywhere Sourcegraph notebooks can be [embedded](/notebooks/notebook-embedding) anywhere that allows iframes. Notebooks hosted on sourcegraph.com can be embedded anywhere. Notebooks hosted on your private instance are subject to your organization's security policies, but can generally be viewed by any user with access to your instance as long as they're logged in. ## Notebook blocks The currently supported block types are: - Query - File - Symbol - Markdown [Read more about block types](/notebooks/blocks). ## Searching notebooks Notebooks created through the web interface are full text searchable from the `/notebooks` page. Each tab has its own search box and each search box is scoped to that tab. The exception is the Explore tab, which searches all notebooks you have access to. Searches will match on notebook titles and any text in blocks. For example any text in Markdown blocks and any of the query text in file, symbol, and search query blocks. Searching through results in symbol, file, and query block types is not supported because they are dynamic in nature. ## Enabling Notebooks in older versions of Sourcegraph In versions older than 3.39 (beginning in 3.36) Notebooks are behind an experimental feature flag. If you're running versions 3.36-3.38 and want to try out Notebooks, enable them in global settings: ``` "experimentalFeatures": { "showSearchNotebook": true } ``` ## Explanations - [Sharing notebooks](/notebooks/notebook-sharing) - [Embedding notebooks](/notebooks/notebook-embedding) - [Block types](/notebooks/blocks) Blocks are the compositional units of a notebook. You can interleave the various block types in a notebook to create rich, powerful documentation. There are four supported block types. # Block types ## Markdown blocks Markdown blocks support standard Markdown formatting, enabling you to create headings, lists, code blocks, and more. They are the foundational block type for providing additional context to the specialized block types described below. ## Query blocks Query blocks support the full Sourcegraph search query language, allowing you to use our full [search syntax](/code-search/queries), including any of our types, filters, patterns, and predicates, to display the most relevant results. > Note: Notebook block searches don't use your Sourcegraph instance's default search configuration. For example you'll need to explicitly specify things like `patterntype:regexp` or `context:sourcegraph` even if your user or global settings have a configured default for these values. ## Symbol blocks With symbol blocks, you can identify the symbol you want to highlight. Symbol blocks are special. As long as the symbol definition stays within the file you selected when you created the block, you don't need to update it when the file changes. Symbol blocks "follow" the symbol around the file and so always display its current location. Symbol blocks provide some UI affordances to make it easier to find symbols, such as a default `type:symbol` in the query, as well as a purpose-built typeahead specialized for symbol selection. ## File blocks File blocks are similar to symbol blocks in that they are some special affordances to make them easier to create. You can add an entire file the file block, or you can select a line range of a file. File ranges are great for embedding code snippets into a notebook or highlighting important files. File blocks are editable so you can modify a full file to only show a line range from it, or remove the line range to show an entire file. If you're viewing a file in Sourcegraph search, you can also copy the URL and paste it directly into a file block or the command palette. If you have a line range selected it will be preserved on paste. # Phabricator integration with Sourcegraph > ⚠️ NOTE: Sourcegraph support of Phabricator is limited, and not expected to evolve due to the [announced](https://admin.phacility.com/phame/post/view/11/phacility_is_winding_down_operations/) cease of support for Phabricator. This Phabricator integration does not support listing and mirroring Phabricator repositories (as it does for repositories on other code hosts). It is intended for use when your repositories are hosted somewhere else (such as GitHub), and Phabricator mirrors repositories from that code host. If your repositories are hosted on Phabricator, you must follow the steps in "[Other Git repository hosts](/admin/code_hosts/other)" to add the repositories so that they are mirrored to Sourcegraph in addition to the steps outlined here to power the integration. Sourcegraph does not currently support using the repository permissions you've set in Phabricator for repositories hosted on Phabricator. Feature | Supported? ------- | ---------- [Repository syncing and mirroring](/admin/code_hosts/phabricator#repository-linking-and-syncing) | ❌ [Repository association](/admin/code_hosts/phabricator#repository-linking-and-syncing) | ✅ [Repository permission syncing](/admin/permissions/syncing) | ❌ [User authentication](/admin/auth/) | ❌ [Browser extension](#browser-extension) | ✅ [Native extension](/admin/code_hosts/phabricator#native-extension) | ✅ ## Repository association Site admins can [associate Phabricator repositories with Sourcegraph](/admin/code_hosts/phabricator#repository-syncing-and-linking). ## Browser extension The [Sourcegraph browser extension](/integration/browser_extension) supports Phabricator. When installed in your web browser, it adds hover tooltips, go-to-definition, find-references, and code search to files and diffs viewed on Phabricator. 1. Install the [Sourcegraph browser extension](/integration/browser_extension). 1. [Configure the browser extension](/integration/browser_extension#configuring-the-sourcegraph-instance-to-use) to use your Sourcegraph instance. 1. To allow the browser extension to work on your Phabricator instance: - Navigate to any page on Phabricator. - Right-click the Sourcegraph icon in the browser extension toolbar. - Click "Enable Sourcegraph on this domain". - Click "Allow" in the permissions request popup. 1. Visit any file or diff on Phabricator. Hover over code or click the "View file" and "View repository" buttons. > NOTE: Site admins can also install the [native Phabricator extension](/admin/code_hosts/phabricator#native-extension) to avoid needing each user to install the browser extension. # Open in Editor With a click of a button, you can jump from any file in Sourcegraph to your preferred editor. It only takes a minute to set up. ## Get started If you haven’t used the feature, find the button on the right side of your screen: When you click it, a small popup appears where you’ll need to set two things: a projects path and your editor. - The **projects path** is the folder where you store your projects. Enter any Linux, Windows, or macOS absolute path. - Example: If your projects path is `/home/username/projects`, one of your repos is https://github.com/sourcegraph/sourcegraph/, and you want to open `README` in the root folder of the repo, then the feature will look for the file at `/home/username/project/sourcegraph/README`. - Then choose your **preferred editor** from the list. We support VS Code, Atom, Sublime Text, and JetBrains IDEs ( AppCode, CLion, GoLand, IntelliJ IDEA, PhpStorm, PyCharm, Rider, RubyMine, WebStorm, and all new IDEs that JetBrains releases). If you use a different editor, check out “Advanced config” below. Once you click **Save**, the icon will you’ll be able to jump to your editor from any file in Sourcegraph. ## Advanced config If you want to dig deeper, there are a bunch of optional settings to play with. Click your name at the top-right, select Settings, and add the `"openInEditor": {}` key to your user setting JSON. - **editorIds:** an array of short names of an editor (“vscode”, “idea”, “sublime”, “pycharm”, etc.) or “custom”. This is what you set in the dropdown when you first set up the feature. - Note that the initial setup UI only allows you to select one editor, but if you use multiple editors, you can manually add more. Each of them will have an icon on your toolbar. - **custom.urlPattern:** If you set editorId to “custom” then this must be set, too. Use the placeholders “%file”, “%line”, and “%col” to mark where the file path, line number, and column number must be inserted. Example URL pattern for IntelliJ IDEA: `idea://open?file=%file&line=%line&column=%col` - **projectPaths.default**: This is what you set in the form when you first set up the feature: The absolute path on your computer where your git repositories live. All git repos to open have to be cloned under this path with their original names. `/Users/yourusername/src` is a valid absolute path, `~/src` is not. Works both with and without a trailing slash. - **projectPaths.linux**: Overwrites the default path on Linux. Handy if you use different environments. Works both with and without a trailing slash. - **projectPaths.mac**: Overwrites the default path on macOS. Works both with and without a trailing slash. - **projectPaths.windows**: Overwrites the default path on Windows. Works both with and without a trailing backslash. - **replacements**: Key-value pairs. Each key will be replaced by the corresponding value in the final URL. Keys are regular expressions, values can contain backreferences ($1, $2, ...). - **jetbrains.forceApi**: Forces using protocol handlers (like `idea://open?file=...`) or the built-in REST API (`http://localhost:63342/api/file...`). If omitted, **protocol handlers** are used if available, otherwise the built-in REST API is used. - **vscode.isProjectPathUNCPath**: Indicates that the given projects path is a UNC (Universal Naming Convention) path. - **vscode.useInsiders**: If set, files will open in VS Code Insiders rather than VS Code. - **vscode.useSSH**: If set, files will open on a remote server via SSH. This requires vscode.remoteHostForSSH to be specified and VS Code extension “Remote Development by Microsoft” installed in your VS Code. - **vscode.remoteHostForSSH**: The remote host as `USER@HOSTNAME`. This needs you to install the extension called “Remote Development by Microsoft” in your VS Code. Just save your settings and enjoy being totally advanced! 😎 ## Examples ### IntelliJ IDEA on macOS To open repository files in your Documents directory: ```json { "openInEditor": { "editorIds": ["idea"], "projectPaths.default": "/Users/USERNAME/Documents" } } ``` ### JetBrains IDEs on Windows **Note:** We talk about IntelliJ here, but it’s similar for all JetBrains IDEs. The `idea://` protocol handler does not always work on Windows. If it fails for you, a workaround is to use IntelliJ’s built-in REST API to open files directly from a URL with some extra configuration steps. Add this to your Sourcegraph _User settings_: ```json { "openInEditor": { ... "jetbrains.forceApi": "builtInServer" } } ``` Then open IntelliJ settings, go to `Build, Execution, Deployment` | `Debugger` | `Built-in Server`, and enable `Allow unsigned requests`. This allows Sourcegraph to make requests to the built-in server, as stated in JetBrains’ [docs](https://www.jetbrains.com/help/idea/php-built-in-web-server.html#configuring-built-in-web-server). **Note:** with this workaround, “Open in Editor” will only work if your IDE is running. ### VS Code on macOS To open repository files in your Documents directory: ```json { "openInEditor": { "editorIds": ["vscode"], "projectPaths.default": "/Users/USERNAME/Documents" } } ``` ### VS Code on Windows To open repository files in your Documents directory: ```json { "openInEditor": { "editorIds": ["vscode"], "projectPaths.default": "C:\Users\USERNAME\Documents" } } ``` ### VS Code on WSL To open repository files in your Home directory: ```json { "openInEditor": { "editorIds": ["vscode"], "projectPaths.default": "//wsl$/Ubuntu-18.04/home" } } ``` ### VS Code Insider on Mac ```json { "openInEditor": { ... "vscode.useInsiders": true } } ``` ### VS Code with different base paths configured for different platforms Uses the assigned path for the detected Operating System when available: ```json { "openInEditor": { "editorIds": ["vscode"], // basePath is required as the default path when no Operating System is detected "projectPaths.default": "/Users/USERNAME/Documents/", "projectPaths.windows": "/C:/Users/USERNAME/folder/", "projectPaths.mac": "/Users/USERNAME/folder/", "projectPaths.linux": "/home/USERNAME/folder/" } } ``` ### Replacement Example 1: Open Remote folders with VS Code on Mac by removing file names **This requires VS Code extension [Remote Development by Microsoft](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) to work.** To open directory where the repository files reside in a remote server: ```json { "openInEditor": { "editorIds": ["custom"], "projectPaths.default": "/Users/USERNAME/Documents/", // Replaces USER@HOSTNAME as appropriate. "custom.urlPattern": "vscode://vscode-remote/ssh-remote+USER@HOSTNAME%file", //removes file name as the vscode-remote protocol handler only supports directory-opening "replacements": { "\/[^\/]*$": "" } } } ``` ### Replacement Example 2: Add string to final file path Adds `sourcegraph-` in front of the string that matches the `(?<=Documents\/)(.*[\\\/])` RegExp pattern, which is the string after `Documents/` and before the final slash. ```json { "openInEditor": { "editorIds": ["vscode"], "projectPaths.default": "/Users/USERNAME/Documents/", "replacements": { "(?<=Documents\/)(.*[\\\/])": "sourcegraph-$1" } // vscode://file//Users/USERNAME/Documents/REPO-NAME/package.json => vscode://file//Users/USERNAME/Documents/sourcegraph-REPO-NAME/package.json } } ``` ### Replacement Example 3: Remove string from the final file path Removes `sourcegraph-` from the final URL. ```json { "openInEditor": { "editorIds": ["vscode"], "projectPaths.default": "/Users/USERNAME/Documents/", "replacements": { "sourcegraph-": "" } // vscode://file//Users/USERNAME/Documents/sourcegraph-REST-OF_REPO-NAME/package.json => vscode://file//Users/USERNAME/Documents/REST-OF_REPO-NAME/package.json } } ``` # Firefox Migration from self-hosted to Add-ons Store We've recently migrated from hosting the Sourcegraph Firefox extension ourselves to hosting it in the Firefox add-ons store, to improve the update process. If you previously installed the Firefox extension from our website, the first time you update it you'll need to reinstall the extension from the Firefox store, which can be done in two quick steps: First, remove your current Sourcegraph extension by right-clicking the Sourcegraph logo and selecting "Remove Extension" from the dropdown. Second, navigate to the [Sourcegraph extension page](https://addons.mozilla.org/en-US/firefox/addon/sourcegraph-for-firefox/) on the Firefox add-ons store and install the latest Sourcegraph extension. Future updates will be distributed through the Firefox store and won't require a re-install. # Integrations Sourcegraph integrates with your other tools to help you search, navigate, and review your code. - [GraphQL API](/api/graphql/): create custom tools using Sourcegraph data - [Browser extension](/integration/browser_extension/): go-to-definitions and hovers in your code host and code reviews - Code hosts - [GitHub](/integration/github) - [GitLab](/integration/gitlab) - [Bitbucket Cloud](/integration/bitbucket_cloud) - [Bitbucket Server / Bitbucket Data Center](/integration/bitbucket_server) - [Other Git repository hosts](/admin/code_hosts/other) - [Editor plugins](/integration/editor): jump to Sourcegraph from your editor - [Open in Editor](/integration/open_in_editor): jump to your editor from Sourcegraph - [Search shortcuts](/integration/browser_extension/how-tos/browser_search_engine): quickly search from your browser - Launcher extensions - [Sourcegraph for Raycast](https://www.raycast.com/bobheadxi/sourcegraph) (unofficial): search code, browse notebooks, and manage batch changes from the Raycast launcher - IDE Extensions - [JetBrains](/integration/jetbrains) - [Manual testing](/integration/jetbrains/manual_testing) - [VS Code](/integration/vscode) ![GitHub pull request integration](https://storage.googleapis.com/sourcegraph-assets/code-graph/docs/github-pr.png) # Go dependencies integration with Sourcegraph You can use Sourcegraph with Go modules from any Go module proxy, including open source code from proxy.golang.org or a private proxy such as [Athens](https://github.com/gomods/athens). This integration makes it possible to search and navigate through the source code of published Go modules (for example, [`gorilla/mux@v1.8.0`](https://sourcegraph.com/go/github.com/gorilla/mux@v1.8.0)). Feature | Supported? ------- | ---------- [Repository syncing](#repository-syncing) | ✅ [Repository permissions](#repository-syncing) | ❌ [Multiple Go repositories code hosts](#multiple-go-dependencies-code-hosts) | ❌ ## Setup See the "[Go dependencies](/admin/code_hosts/go)" documentation. ## Repository syncing Site admins can [add Go packages to Sourcegraph](/admin/code_hosts/go#repository-syncing). ## Repository permissions ⚠️ Go dependency repositories are visible by all users of the Sourcegraph instance. ## Multiple Go dependencies code hosts ⚠️ It's only possible to create one Ruby dependency code host for each Sourcegraph instance. # Sourcegraph extensions on Gitpod You can use our Sourcegraph IDE extensions in your [Gitpod](https://gitpod.io/) workspace. ### Installation guide Our IDE extensions work with the following IDEs supported by Gitpod. #### Gitpod VS Code Desktop 1. Open the `Extensions view` by clicking on the Extensions icon in the [Activity Bar](https://code.visualstudio.com/api/ux-guidelines/overview#activity-bar) on the side of your Gitpod VS Code workspace 2. Search and install our [Sourcegraph](https://open-vsx.org/extension/sourcegraph/sourcegraph) #### Gitpod VS Code for the Web 1. Open the `Extensions view` by clicking on the Extensions icon in the [Activity Bar](https://code.visualstudio.com/api/ux-guidelines/overview#activity-bar) on the side of your Gitpod VS Code for the Web workspace 2. Search and install our [Sourcegraph](https://open-vsx.org/extension/sourcegraph/sourcegraph) #### Gitpod JetBrains IDEs 1. Follow the instructions in [Gitpod's JetBrains IDE support docs](https://www.gitpod.io/docs/ides-and-editors/jetbrains-gateway#jetbrains-ide-support) to setup your workspace. 2. In your Gitpod JetBrains IDE workspace, select `Preferences` 3. Select `Plugins: on host` 4. Search and install `Sourcegraph` # Gitolite integration with Sourcegraph You can use Sourcegraph with [Gitolite](http://gitolite.com/). Feature | Supported? ------- | ---------- [Repository syncing](/admin/code_hosts/gitolite#repository-syncing) | ✅ [Browser extension](#browser-extension) | ❌ ## Repository syncing Site admins can [sync Gitolite repositories to Sourcegraph](/admin/code_hosts/gitolite#repository-syncing). ## Browser extension The [Sourcegraph browser extension](/integration/browser_extension) does not support Gitolite because Gitolite has no web interface. If your Gitolite repositories are also available on another code host (such as GitHub or Phabricator), you can use the browser extension with those services. # GitLab integration with Sourcegraph You can use Sourcegraph with [GitLab.com](https://gitlab.com) and GitLab CE/EE. Read a high-level overview of how Sourcegraph and GitLab work together in our [GitLab solution brief](https://about.sourcegraph.com/guides/sourcegraph-gitlab-solution-brief.pdf). Feature | Supported? ------- | ---------- [Repository syncing](/admin/code_hosts/gitlab#repository-syncing) | ✅ [Repository permissions](/admin/code_hosts/gitlab#repository-permissions) | ✅ [User authentication](/admin/code_hosts/gitlab#user-authentication) | ✅ [GitLab UI native integration](#gitlab-ui-native-integration) | ✅ [Browser extension](#browser-extension) | ✅ ## Repository syncing Site admins can [add GitLab repositories to Sourcegraph](/admin/code_hosts/gitlab#repository-syncing). ## Repository permissions Site admins can [configure Sourcegraph to respect GitLab repository access permissions](/admin/code_hosts/gitlab#repository-permissions). ## User authentication Site admins can [configure Sourcegraph to allow users to sign in via GitLab](/admin/code_hosts/gitlab#user-authentication). ## GitLab UI native integration GitLab instances can be configured to show Sourcegraph code navigation natively. See the [GitLab integration docs](/admin/code_hosts/gitlab#native-integration) for how to enable this on your GitLab instance. ![GitLab native integration](img/gitlab-code-intel.gif) ## Browser extension The [Sourcegraph browser extension](/integration/browser_extension) supports GitLab. When installed in your web browser, it adds hover tooltips, go-to-definition, find-references, and code search to files and merge requests viewed on GitLab. 1. Install the [Sourcegraph browser extension](/integration/browser_extension). 1. [Configure the browser extension](/integration/browser_extension#configuring-the-sourcegraph-instance-to-use) to use your Sourcegraph instance. - You can also use [`https://sourcegraph.com`](https://sourcegraph.com) for public code from GitLab.com only. 1. To allow the browser extension to work on your GitLab instance: - Navigate to any page on GitLab. - Right-click the Sourcegraph icon in the browser extension toolbar. - Click "Enable Sourcegraph on this domain". - Click "Allow" in the permissions request popup. 1. Visit any file or merge request on GitLab. Hover over code or click the "View file" and "View repository" buttons. # GitHub integration with Sourcegraph You can use Sourcegraph with [GitHub.com](https://github.com) and [GitHub Enterprise](https://enterprise.github.com). Feature | Supported? ------- | ---------- [Repository syncing](/admin/code_hosts/github#selecting-repositories-to-sync) | ✅ [Repository permissions](/admin/code_hosts/github#repository-permissions) | ✅ [User authentication](/admin/code_hosts/github#user-authentication) | ✅ [Browser extension](#browser-extension) | ✅ ## Repository syncing Site admins can [add GitHub repositories to Sourcegraph](/admin/code_hosts/github#selecting-repositories-to-sync). ## Repository permissions Site admins can [configure Sourcegraph to respect GitHub repository access permissions](/admin/code_hosts/github#repository-permissions). ## User authentication Site admins can [configure Sourcegraph to allow users to sign in via GitHub](/admin/code_hosts/github#user-authentication). ## Browser extension The [Sourcegraph browser extension](/integration/browser_extension) supports GitHub. When installed in your web browser, it adds hover tooltips, go-to-definition, find-references, and code search to files and pull requests viewed on GitHub and GitHub Enterprise. 1. Install the [Sourcegraph browser extension](/integration/browser_extension). 1. [Configure the browser extension](/integration/browser_extension#configuring-the-sourcegraph-instance-to-use) to use your Sourcegraph instance. - You can also use [`https://sourcegraph.com`](https://sourcegraph.com) for public code only. 1. GitHub Enterprise only: Click the Sourcegraph icon in the browser toolbar to open the settings page. If a permissions notice is displayed, click **Grant permissions** to allow the browser extension to work on your GitHub Enterprise instance. 1. Visit any file or pull request on GitHub. Hover over code or click the "View file" and "View repository" buttons. # Editor integrations Sourcegraph's editor integrations allow you search and navigate across all of your repositories without ever leaving your IDE or checking them out locally. Get up to speed quickly, learn best practices and, find code to re-use all while maintaining your flow using our editor integrations. We currently have integrations with these editors: - [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=sourcegraph.sourcegraph) - [JetBrains IDEs (IntelliJ, PyCharm, GoLand, etc.)](https://plugins.jetbrains.com/plugin/9682-sourcegraph) - [Gitpod](https://open-vsx.org/extension/sourcegraph/sourcegraph) ## References - [Installation guide for Gitpod](/integration/gitpod) - [Sourcegraph on Open VSX Registry](https://open-vsx.org/extension/sourcegraph/sourcegraph) # Bitbucket Server / Bitbucket Data Center integration with Sourcegraph You can use Sourcegraph with Git repositories hosted on [Bitbucket Server](https://www.atlassian.com/software/bitbucket/server) (and the [Bitbucket Data Center](https://www.atlassian.com/enterprise/data-center/bitbucket) deployment option). Feature | Supported? ------- | ---------- [Repository syncing](/admin/code_hosts/bitbucket_server) | ✅ [Webhooks](/admin/code_hosts/bitbucket_server#webhooks) | ✅ [Repository permissions](/admin/code_hosts/bitbucket_server#repository-permissions) | ✅ [Sourcegraph Bitbucket Server plugin](#sourcegraph-bitbucket-server-plugin) | ✅ [Browser extension](#browser-extension) | ✅ ## Repository syncing Site admins can [add Bitbucket Server / Bitbucket Data Center repositories to Sourcegraph](/admin/code_hosts/bitbucket_server). ## Repository permissions Site admins can [configure Sourcegraph to respect Bitbucket Server / Bitbucket Data Center's repository access permissions](/admin/code_hosts/bitbucket_server#repository-permissions). ## Sourcegraph Bitbucket Plugin We recommend installing the Sourcegraph Bitbucket plugin which adds the following features to your Bitbucket Server / Bitbucket Data Center instance: - **Fast permission syncing** between Sourcegraph and Bitbucket Server / Bitbucket Data Center - **Webhooks with configurable scope**, which are used by and highly recommended for usage with [batch changes](/batch-changes/) ### Installation and requirements Install the Sourcegraph plugin for Bitbucket from the [Atlassian Marketplace](https://marketplace.atlassian.com/apps/1231975/sourcegraph-for-bitbucket?tab=overview&hosting=datacenter) or see the [bitbucket-server-plugin](https://github.com/sourcegraph/bitbucket-server-plugin) repository for instructions on how to manually install the plugin on your Bitbucket Server / Bitbucket Data Center instance. For the Bitbucket Server plugin to then communicate with the Sourcegraph instance, the Sourcegraph site configuration must be updated to include the [`corsOrigin` property](/admin/config/site_config) with the Bitbucket Server / Bitbucket Data Center URL ```json { // ... "corsOrigin": "https://my-bitbucket.example.com" // ... } ``` #### I am seeing Content Security Policy violations, what should I do? If you have a Content Security Policy (CSP) setup in place and have encountered issues, you may need to adjust the CSP for our Bitbucket Server plugin. To ensure the proper functioning of our Bitbucket Server plugin, add your Sourcegraph instance URL to the following CSP directives: - `script-src` - `style-src` - `connect-src` - `frame-src` Here's an example of how to add your Sourcegraph instance URL to the required CSP directives. In this case, the instance URL is `example.com`. Make sure not to remove any existing values in the directives; just append the Sourcegraph URL. ``` script-src 'self' 'unsafe-inline' example.com; style-src 'self' 'unsafe-inline' example.com; connect-src 'self' example.com; frame-src 'self' example.com; ``` It is not ideal to include `unsafe-inline` here for `script-src`, but it is the recommended approach from Bitbucket. This is a [known issue](https://community.developer.atlassian.com/t/bitbucket-connect-library-uses-eval-preveting-to-use-csp-without-unsafe-inline-as-required/60522) with Bitbucket that is unrelated to our plugin. By following these steps, you should be able to resolve the CSP violations for our Bitbucket Server plugin. If you have any questions or need further assistance, please feel free to reach out to our support team. ### Updating In order to update the plugin, follow the same steps as for installing it, which are described in the [bitbucket-server-plugin](https://github.com/sourcegraph/bitbucket-server-plugin) repository. When the Sourcegraph instance connected to the Bitbucket Server plugin is updated, so will the code that's fetched by the plugin to enable native code navigation. No manual steps required. (See the [Technical Details](#technical-details) section on how this works.) ### Webhooks Once the plugin is installed, go to **Administration > Add-ons > Sourcegraph** to see a list of all configured webhooks and to create a new one. To configure a webhook, follow [these steps](/admin/code_hosts/bitbucket_server#webhooks). Disabling the webhook is as easy as removing the `"webhooks"` property from the `"plugin"` section and deleting the webhook pointing to your Sourcegraph instance under **Administration > Add-ons > Sourcegraph**. > NOTE: Version 1.3.3 or higher of the plugin is required for Build Status support. ### Fast permission syncing The plugin also supports an optional method of faster ACL permissions syncing that aims to improve the speed of fetching a user's permissions from Bitbucket (which can reduce the time a user has to wait to run a search if their permissions data has expired). You can enable this feature when [configuring the connection to your Bitbucket Server / Bitbucket Data Center instance on Sourcegraph](/admin/code_hosts/bitbucket_server#repository-permissions). For more information on when permissions are fetched, how long they're cached and how to configure that behavior, see our documentation on [Repository permissions](/admin/permissions/). The speed improvements are most important on larger Bitbucket Server / Bitbucket Data Center instances with thousands of repositories. When connected to these instances, Sourcegraph would have to make many wasteful requests to fetch permission data if the plugin is not installed. To learn how and why this works, read the [through technical details of fast permission syncing](#technical-details) below. ### Technical Details This section provides some technical insight into the Bitbucket Server plugin to make it easier to users to decide whether or not to install it on their Bitbucket Server / Bitbucket Data Center instance. You can find the full source code for the plugin at [github.com/sourcegraph/bitbucket-server-plugin](https://github.com/sourcegraph/bitbucket-server-plugin/). #### Webhooks Bitbucket Server / Bitbucket Data Center natively only [provides **per-repository** webhooks](https://confluence.atlassian.com/bitbucketserver/managing-webhooks-in-bitbucket-server-938025878.html). Sourcegraph's Bitbucket Server / Bitbucket Data Center adds support for webhooks with a **configurable scope**. Each webhook can be configured to listen to specific events **globally**, per **project** or per **repository**. The motivation behind this added functionality is to more efficiently react to updates to Bitbucket Server / Bitbucket Data Center pull requests when using [batch changes](/batch-changes/) by requiring only a single webhook to receive events for hundreds or thousands of pull requests across projects and repositories. The plugin adds a `/webhook` endpoint that accepts `GET`, `POST` and `DELETE` HTTP request to list, create and delete webhooks respectively. The full URL for this endpoint would be something like `https://your-bbs-instance.example.com/rest/sourcegraph-admin/1.0/webhook`. See the [webhooks README](https://github.com/sourcegraph/bitbucket-server-plugin/blob/master/src/main/java/com/sourcegraph/webhook/README) for detailed information on which payloads this endpoint accepts. Once the plugin is installed it registers an asynchronous listener (see [`WebhookListener.java`](https://github.com/sourcegraph/bitbucket-server-plugin/blob/master/src/main/java/com/sourcegraph/webhook/WebhookListener.java)) that listens to `PullRequestEvent`s and `BuildStatusEvent`s. When an event is dispatched to the listener it checks whether a webhook has been registered for the scope and type of the event and if so, it enqueues the sending of a request to the webhook's endpoint in a thread pool. (See [`WebhookListener.handle`](https://github.com/sourcegraph/bitbucket-server-plugin/blob/master/src/main/java/com/sourcegraph/webhook/WebhookListener.java#L62-L76) and [`Dispatcher.java`](https://github.com/sourcegraph/bitbucket-server-plugin/blob/master/src/main/java/com/sourcegraph/webhook/Dispatcher.java).) In order to persist the configured webhooks across restarts of the Bitbucket Server / Bitbucket Data Center instance the plugin uses the [Active Objects ORM](https://developer.atlassian.com/server/framework/atlassian-sdk/active-objects/) of the Atlassian SDK. It registers two Active Objects: [`WebhookEntity` and `EventEntity`](https://github.com/sourcegraph/bitbucket-server-plugin/blob/94e4be96b57286429cc543205164586af03e9b9b/src/main/resources/atlassian-plugin.xml#L10-L14). If Sourcegraph is configured to make use of the Bitbucket Server plugin webhooks (which is done by setting the [`"plugin.webhooks"` property in the Bitbucket Server / Bitbucket Data Center configuration](/admin/code_hosts/bitbucket_server#webhooks)), it sends a request to the Bitbucket Server / Bitbucket Data Center instance, every minute, to make sure that a webhook on the Bitbucket Server / Bitbucket Data Center instance exists and points to the Sourcegraph instance. #### Fast permission syncing When Sourcegraph is configured to use [Bitbucket Server / Bitbucket Data Center's repository permissions](/admin/code_hosts/bitbucket_server#repository-permissions) to control access to repositories on Sourcegraph, it needs to fetch permissions for each user. The Bitbucket Server / Bitbucket Data Center REST API only provides **paginated** endpoints to fetch either the list of repositories a given user has access to, or the list of users that have access to a given repository. Both endpoints return the **full representation of the entities**. Since Sourcegraph is only interested in the IDs of either repositories or users (those are already synced to its database) the Bitbucket Server plugins adds two REST endpoints that only return IDs to provide a more efficient way of fetching permission data: - `/permissions/repositories?user=&permission=`
Returns **a list of repository IDs** the given `user` has access to on the given `permission` level. - `/permissions/users?repository=&permission=`
Returns **a list of user IDs** that have access to the given `repository` on the given `permission` level. The lists returned by both endpoints are encoded as [Roaring Bitmaps](https://roaringbitmap.org/). Since only a single request is required to fetch the complete list of desired IDs and the response contains only IDs, encoded in an efficient binary format, these two endpoints make the fetching of permissions roughly **eight times faster** (measured on an instance with 10000 repositories) than using Bitbucket Server / Bitbucket Data Center's REST API. The feature is opt-in on Sourcegraph's side but the endpoints are enabled by default on Bitbucket Server / Bitbucket Data Center, which makes it easy to load-test and benchmark the endpoints with a single request. For example, in order to fetch all the repository IDs a single user has access to: ``` curl 'https://bitbucket.example.com/rest/sourcegraph-admin/1.0/permissions/repositories?user=USERNAME&permission=read' -X GET -H 'Authorization: Bearer YOUR_TOKEN' --output /dev/null ``` In our tests we could see that using these endpoints reduces the overall load on the instance, because instead of doing `/1000` requests that take 600-900ms each, Sourcegraph would only do a single request that takes 1000-1500ms. Bitbucket Server / Bitbucket Data Center admins can further increase the performance of these endpoints by increasing the [`page.max.repositories` property in the Bitbucket Server / Bitbucket Data Center configuration](https://confluence.atlassian.com/bitbucketserver/bitbucket-server-config-properties-776640155.html#BitbucketServerconfigproperties-Paging), but you should check your other Bitbucket Server plugins will not be adversely affected by increasing this. The plugin uses `RepositoryService`, `UserManager`, `UserService` and `SecurityService` provided by the Atlassian SDK to fetch users or repositories from Bitbucket Server / Bitbucket Data Center's database. You can see the full code for these two endpoints in [`PermissionRouter.java`](https://github.com/sourcegraph/bitbucket-server-plugin/blob/master/src/main/java/com/sourcegraph/permission/PermissionRouter.java) ## Browser extension The [Sourcegraph browser extension](/integration/browser_extension) supports Bitbucket Server / Bitbucket Data Center. When installed in your web browser, it adds hover tooltips, go-to-definition, find-references, and code search to files and pull requests viewed on Bitbucket Server / Bitbucket Data Center. 1. Install the [Sourcegraph browser extension](/integration/browser_extension). 1. [Configure the browser extension](/integration/browser_extension#configuring-the-sourcegraph-instance-to-use) to use your Sourcegraph instance. 1. To allow the browser extension to work on your Bitbucket Server / Bitbucket Data Center instance: - Navigate to any page on Bitbucket Server / Bitbucket Data Center. - Right-click the Sourcegraph icon in the browser extension toolbar. - Click "Enable Sourcegraph on this domain". - Click "Allow" in the permissions request popup. 1. Visit any file or pull request on Bitbucket Server / Bitbucket Data Center. Hover over code or click the "View file" and "View repository" buttons.
# Bitbucket Cloud integration with Sourcegraph You can use Sourcegraph with Git repositories hosted on [Bitbucket Cloud](https://bitbucket.org). Feature | Supported? ------- | ---------- [Repository syncing](/admin/code_hosts/bitbucket_cloud) | ✅ [Repository permissions](/admin/code_hosts/bitbucket_cloud#repository-permissions) | ✅ Browser extension | ✅ Native extension | ❌ Not supported on Bitbucket.org ## Repository syncing Site admins can [add Bitbucket Cloud repositories to Sourcegraph](/admin/code_hosts/bitbucket_cloud). ## User authorization Site admins can [add Bitbucket Cloud as an authentication provider to Sourcegraph](/admin/auth#bitbucket-cloud). This will allow users to sign into Sourcegraph using their Bitbucket Cloud accounts. Site admins can then also [enable repository permissions](/admin/code_hosts/bitbucket_cloud#repository-permissions) on their Bitbucket Cloud code host connections. # AWS CodeCommit integration with Sourcegraph You can use Sourcegraph with Git repositories hosted on [AWS CodeCommit](https://aws.amazon.com/codecommit/). Feature | Supported? ------- | ---------- [Repository syncing](/admin/code_hosts/aws_codecommit) | ✅ [Browser extension](/integration/browser_extension) | ❌ ## Repository syncing Site admins can [add AWS CodeCommit repositories to Sourcegraph](/admin/code_hosts/aws_codecommit). ## Browser extension The [Sourcegraph browser extension](/integration/browser_extension) does not yet support AWS CodeCommit. This means that you won't get hovers, go-to-definition, and find-references from Sourcegraph when viewing your code on AWS CodeCommit's web interface. # VS Code extension - [See user docs](/cody/clients/install-vscode) # JetBrains plugin manual test cases ## Overview All these test scripts are written in a way so that humans can perform it, but also an E2E testing tool can run it eventually. Each of these test cases should be run either right after starting the IDE and opening a project, _or_ after closing and reopening a project, to make sure we have a clean state. ### Test case format anatomy * The numbered items are the steps to perform in order. * The indented bullets are assertions. ### Performing a test manually If a red error notification appears at the bottom right of the IDE, that means we have a problem. Same if any of the assertions fail. If there is a problem, we should either investigate right away (typically, if a developer does the manual testing), or create an issue with the error message / specifying which test case failed, and any additional helpful context. ### Operating systems We test on macOS by default, but we should also test on Windows and Linux more often. We have three test instances to test the plugin on a Linux server, an Ubuntu Desktop machine, and on Windows Server. They are all on GCP, [here](https://console.cloud.google.com/compute/instances?cloudshell=true&project=david-veszelovszki-temp-env). - For the Windows instance, follow [this guide](https://www.snel.com/support/how-to-connect-to-your-server-via-rdp-on-macos/) to connect, then use the login/password you find in 1Password by the name "GCP: JetBrains plugin testing Windows". ## Test cases ### Website-related features #### Preparation **Perforce** testing is slightly tricky because we don’t currently have a lot of Perforce code hosts added to any of our Sourcegraph instances. Try this: * Use [https://cse-k8s.sgdev.org/](https://cse-k8s.sgdev.org/) as your Sourcegraph URL * Generate an access token for yourself for CSE-K8S (admin user in 1Password), set that * Set up a Perforce client * Set `perforce-tests.sgdev.org:1666` for the port. User is “admin”, password is in 1Password. * Make sure your connection work, and _actually connect_ in P4V (connection didn’t work without this step in IntelliJ for me) * Create a workspace for yourself (delete an old one if you bump into the 20-workspace limit), and include the `test-large-depot` depot * In the JetBrains plugin settings, set the replacement string to `perforce-tests.sgdev.org:,perforce.beatrix.com:app/,.perforce,/patch/core.perforce`. * At this point, your files should open properly in your browser. #### Search Selection and Open/Copy features 1. Open a project with a file that’s under Git version control 2. Right click on editor | Sourcegraph | Search Selection on Sourcegraph Web * Make sure it searches the current selection in all repos 3. Right click on editor | Sourcegraph | Search Selection in Repository on Sourcegraph Web * Make sure it searches the current selection in the current repo 4. Right click on editor | Sourcegraph | Open Selection in Sourcegraph Web * Make sure it opens the right file on the web 5. Copy Sourcegraph File Link * Make sure it copies the right URL (should be good if the previous point worked because they share the logic.) 6. Repeat it with a Perforce repo. #### “Open Revision Diff in Sourcegraph Web” 1. Open a project that has files under Git version control 2. Open Version Control (⌘9) | Log 3. Right click on a commit and choose Open Revision Diff in Sourcegraph Web 4. Make sure the right page opens on the Sourcegraph instance set up in the plugin settings 5. (Would be cool to also test this each time with a project that has files from multiple repositories.) 6. Repeat the process with a project that has files under Perforce version control. ### Find with Sourcegraph #### Popup opens, loads, and closes Why: To make sure the popup is discoverable and generally works 1. Press the shortcut for the popup * The popup should become active * The popup should be in the “loading” state 2. Wait until the “loading” state is gone * It should not take more than ~five seconds 3. Close the popup with ESC * The popup should become hidden 4. Open “Find Action” (⌘⇧A) and search for “find with sourcegraph” and select the first result * The popup should become active * The popup should not be in the “loading” state 5. Click the header of the IDE main window * The popup should become hidden 6. In the main menu, choose Edit | Find | Find on Sourcegraph… * The popup should become active #### Browser doesn’t disappear after a few opens Why: After opening and closing the "Find on Sourcegraph" popup four-five times on Mac, the browser disappears if _circumstances are not right_. This bug should be fixed, but it may reappear if we mess something up. 1. Open popup with the shortcut * The popup should become active 2. Wait until the “loading” state is gone 3. Close the popup with ESC 4. Open popup with the shortcut * The popup should become active, and the browser should be visible 5. Repeat 3–4 at least five times. #### Search results are displayed and navigation works Why: To make sure that the most basic functionality of the popup works 1. Open the popup with the shortcut 2. Wait for the browser content to load 3. Type “repo:^github\.com/sourcegraph/sourcegraph file:index.ts” * It should type into the input box * The content should be exactly the same as what was typed 4. Press Enter 5. Wait for the search results and preview to load * The result list and the preview box should populate 6. Press `↓` key 7. Wait for the search results and preview to load * The preview should be different from the previous one 8. Press `⌥Enter` * The popup should close * A new editor tab should open * The content of the new tab should be the same as the last preview 9. Open the popup with the shortcut 10. Press ⌘A, type “repo:^github\.com/sourcegraph/sourcegraph” and press Enter 11. Wait for the search results to load * The result list should update * The preview state should be “No preview available” 12. Press ⌥Enter * The popup should close * A browser should open (if automated: an “open browser” trigger should be sent) #### Server connection success/failure is recognized This test needs a valid access token that can be generated at https://sourcegraph.com/users/{username}/settings/tokens * Open Settings with `⌘`, * Go to Tools | Sourcegraph * Set the URL to “[https://sourcegraph.com](https://sourcegraph.com)” * Set the access token to a valid one * Press Enter to save settings * Open the popup with the shortcut * Wait for the browser content to load. * The search box should be visible * Close the popup with ESC * Open Settings with ⌘, * Type “TEST” after the end of the valid access token. * Press Enter to save settings * Open the popup with the shortcut * The search box should not be visible, an error message should appear * The preview should be saying “No preview available” * Close the popup with ESC * Open Settings with ⌘, * Remove “TEST” to again have the valid access token. * Press Enter to save settings * Open the popup with the shortcut * The search box should be visible again. # JetBrains plugin - See user docs at [jetbrains/README](https://sourcegraph.com/github.com/sourcegraph/jetbrains/-/tree/README) - See dev docs at [jetbrains/CONTRIBUTING](https://sourcegraph.com/github.com/sourcegraph/jetbrains/-/tree/CONTRIBUTING) - [Manual test cases](/integration/jetbrains/manual_testing) # Quickstart for Browser Extensions Get started with our Browser Extension in 5 minutes or less ## Installation Use one of the following links to install the Sourcegraph Browser Extension for your favourite browser. - [Install Sourcegraph for Chrome](https://chrome.google.com/webstore/detail/sourcegraph/dgjhfomjieaadpoljlnidmbgkdffpack) - [Install Sourcegraph for Safari](https://apps.apple.com/us/app/sourcegraph-for-safari/id1543262193) - [Install Sourcegraph for Firefox](https://addons.mozilla.org/en-US/firefox/addon/sourcegraph-for-firefox) >NOTE: If you were using our self-hosted version of Firefox Extension and are looking to upgrade, please check our [migration guide](/integration/migrating_firefox_extension). ## Configure to use your Sourcegraph instance By default, the browser extension communicates with [sourcegraph.com](https://sourcegraph.com), which has only public code. To use the browser extension with your private repositories, you need to set up a private Sourcegraph instance and connect the extension to it. Follow these instructions: 1. [Install Sourcegraph](/admin/install). Skip this step if you already have a private Sourcegraph instance. 2. Click the Sourcegraph extension icon in the browser toolbar on the browser tab for your private Sourcegraph instance then open the settings page. 3. Enter the URL (including the protocol) of your Sourcegraph instance (such as `https://sourcegraph.example.com`) 4. Make sure the connection status shows "Looks good!" ## Grant permissions for your code host - [GitHub.com](https://github.com/) - no action required. - GitHub Enterprise, GitLab, Bitbucket Server, Bitbucket Data and Phabricator - you need to grant the extension permissions first. To grant these permissions: 1. Navigate to any page on your code host. 2. Right-click the Sourcegraph icon in the browser extension toolbar. 3. Click "Enable Sourcegraph on this domain". 4. Click "Allow" in the permissions request popup. # Browser Extensions

Bring the power of Sourcegraph to your code host

The Sourcegraph browser extension allows you to open repos, compare revisions and search code directly from Chrome's Omnibox for faster development on GitHub and GitHub Enterprise Server with support coming to GitLab, Phabricator, Bitbucket Server and Bitbucket Data Center in the future. - [Install Sourcegraph for Chrome](https://chrome.google.com/webstore/detail/sourcegraph/dgjhfomjieaadpoljlnidmbgkdffpack) - [Install Sourcegraph for Safari](https://apps.apple.com/us/app/sourcegraph-for-safari/id1543262193) - [Install Sourcegraph for Firefox](https://addons.mozilla.org/en-US/firefox/addon/sourcegraph-for-firefox) ## Recommended ## How-tos - [Install across all Google Workspace users](/integration/browser_extension/how-tos/google_workspace) - [Add browser search engine shortcuts](/integration/browser_extension/how-tos/browser_search_engine) - [Troubleshoot common issues](/integration/browser_extension/how-tos/troubleshooting) ## References - [Privacy](/integration/browser_extension/references/privacy)
# Privacy Sourcegraph integrations will only connect to Sourcegraph.com as required to provide code navigation or other functionality on public code. As a result, no private code, private repository names, usernames, or any other specific data is sent to Sourcegraph.com. If connected to a **private, self-hosted Sourcegraph instance**, Sourcegraph integrations never send any logs, pings, usage statistics, or telemetry to Sourcegraph.com. They will send notifications of usage to that private Sourcegraph instance only. This allows the site admins to see usage statistics. If connected to the **public Sourcegraph.com instance**, Sourcegraph integrations will send notifications of usage on public repositories to Sourcegraph.com. The browser extension also does not store sensitive data locally. The information stored is restricted to: - AnonymousUid - Feature flags - Client settings - Enable/disable status - Sourcegraph URL # References The following is a list of reference documents for Browser Extensions: - [Features](/integration/browser_extension/references/features) - [Privacy](/integration/browser_extension/references/privacy) # Features ## Code navigation When you hover your mouse over code in files, diffs, pull requests, etc., the Sourcegraph extension displays a tooltip with: - Documentation and the type signature for the hovered token - **Go to definition** button - **Find references** button ## Search shortcut in URL location bar The Sourcegraph extension adds a search engine shortcut to your web browser that performs a search on your Sourcegraph instance. After you've installed it (see above), use the search shortcut it provides to perform a search: 1. Place cursor in the URL location bar, then type src Tab. 2. Start typing your search query. 3. Select an instant search suggestion or press Enter to see all results. To install this search engine shortcut manually, and for more information, see "[Browser search engine shortcuts](/integration/browser_extension/how-tos/browser_search_engine)". # Troubleshooting Below are the most common issues and how to resolve them. ## No code navigation or buttons ("View repository", "View file", etc.) are displayed on the code host ![Browser extension not working on code host](https://sourcegraphstatic.com/BrowserExtensionNotWorkingCodeHost.gif) Try the following: 1. Click the Sourcegraph extension icon in the browser toolbar to open the settings page. - Ensure that the Sourcegraph URL is correct. It must point to your own Sourcegraph instance to work on private code. - Check whether any permissions must be granted. If so, the settings page will offer you to "grant the Sourcegraph browser extension additional permissions". 1. On some code hosts, you need to be signed in (to the code host) to use the browser extension. Try signing in. ## The *Enable Sourcegraph on this domain* option is not available In rare cases, Chrome can get into the state where the option to **Enable Sourcegraph on this domain** is not available when right-clicking on the extension icon. One fix we've observed is to toggle the site access from on, to off, then on again (see below). ![Toggle site access for browser extension](https://sourcegraphstatic.com/ToggleSiteAccess.gif) If that still doesn't work, viewing the console and network activity of the extension is the next step. ## Viewing browser extension console and network activity in Chrome and Safari If you are still experiencing issues, the next step is to inspect the browser extension console output and network activity, often revealing subtle configuration errors. ![Chrome extension console and network activity](https://sourcegraphstatic.com/ChromeExtensionConsoleNetworkActivity.gif) In Chrome: 1. Right click the Sourcegraph browser extension icon 2. Select Manage Extensions 3. Under Inspect Views select background page, this will open a dev console to the extension background page 4. In the developer console select the network tab In Safari: 1. Ensure you have access to the develop tab by selecting Safari > Preferences > Advanced, at the bottom of the preference UI check the box labelled `Show Develop menu in menu bar` 2. In Develop select Web Extension Background Pages > Sourcegraph 3. Select the Network tab If that still doesn't help, take a screenshot of the console and network activity and [report here](https://help.sourcegraph.com/hc/en-us/requests/new) so we can investigate further. ## Unable to connect to `http://...` Ensure the URL is correct and you are logged in Since `v3.14.0+`, the Sourcegraph browser extension can only authenticate with Sourcegraph instances that have [HTTPS](/admin/tls_ssl) configured. Previously, the Sourcegraph browser extension was able to authenticate with instances that hadn't enabled tls / ssl. However, modern web browsers have started to adopt and implement [an IETF proposal](https://web.dev/samesite-cookies-explained/) that removes the deprecated logic that allowed this behavior. Please configure [HTTPS](/admin/tls_ssl) in order to continue using the browser extension with your private instance. ## `The string did not match the expected pattern` error in Safari The above validation error occurs in Safari when the browser tries to parse a non-JSON text as JSON. Communication between the graphql server & browser extension is always assumed to be JSON. However, when a proxy/saml is used with Sourcegraph, sometimes the proxy can send text/html instead. If you see this error displayed beneath the Url field of the sourcegraph extension, make sure that your current browser session is not in private mode. # How-tos The following is a list of how-to documents for Browser Extensions: - [Install across all Google Workspace users](/integration/browser_extension/how-tos/google_workspace) - [Troubleshooting](/integration/browser_extension/how-tos/troubleshooting) - [Add browser search engine shortcuts](/integration/browser_extension/how-tos/browser_search_engine) # Google Workspace and Chrome Enterprise integration > NOTE: Company-wide deployment via Google Workspace is a [paid upgrade](https://about.sourcegraph.com/pricing) You can install and preconfigure the Sourcegraph Chrome extension for all members of a Google Workspace organization or organizational unit, so that they can get code navigation on files and diffs. By default, the browser extension only has access to github.com. Access to additional sites is granted by the user, on a site-by-site basis. The Sourcegraph browser extension is open source and never sends any logs, pings, usage statistics or telemetry to Sourcegraph.com. Read more about browser extension privacy [here](/integration/browser_extension/references/privacy). ## Automatically install with Google Workspace Using the Google Admin Console, you can [automatically install](https://support.google.com/chrome/a/answer/6306504?hl=en) the Sourcegraph browser extension for all users in your organization. To do so: 1. Visit the Google Admin Console [Chrome management](https://admin.google.com/u/2/ac/chrome/apps/user) page, and click "Apps and extensions". 2. Select the desired organizational unit in the left-hand menu. 3. Click the **+** button to add an extension, select "Add Chrome app or extension by ID", and paste in the ID of the Sourcegraph browser extension: `dgjhfomjieaadpoljlnidmbgkdffpack`. 4. In "Installation Policy", select "Force install". 5. In the "Policy for extensions" field, fill in a JSON value containing the URL of your self-hosted Sourcegraph instance: ``` { "sourcegraphURL": { "Value": "https://sourcegraph.example.com" } } ``` 6. Click "Save". The Sourcegraph browser extension will then be rolled out to all managed browsers in your organization, preconfigured to point to your self-hosted Sourcegraph instance. The roll-out may take some time. Users will need to have valid accounts and be signed in to your Sourcegraph instance for the browser extension to provide code navigation. Users may also need to grant additional permissions to the Sourcegraph browser extension in order for it to provide code navigation on your code host. To check if Sourcegraph is properly configured, navigate to any page on your code host, and click the Sourcegraph icon in the browser toolbar. The popup will prompt you to grant additional permissions if needed: grant_permissions Users can disable Sourcegraph at any time by using the toggle in the extension settings: disable_extension # Browser search engine shortcuts You can add Sourcegraph as a browser search engine to quickly search Sourcegraph from your browser's location bar when you type a special shortcut, such as `src`. This is one of the most common ways to initiate a search on Sourcegraph. ![](https://sourcegraphstatic.com/SearchShortcut2.gif) --- ## Adding Sourcegraph as a browser search engine ### With the Sourcegraph browser extension (recommended) The easiest way is to install the [Sourcegraph browser extension](/integration/browser_extension/quickstart), which automatically configures the location bar `src` shortcut to search Sourcegraph. ### Google Chrome 1. Perform a code search on the Sourcegraph instance you want to add as a search engine. 1. Copy the URL of the search results page. 1. Go to `chrome://settings`. Under **Search engine**, click **Manage search engines**. Next to **Other search engines**, click **ADD**. ![](https://storage.googleapis.com/sourcegraph-assets/Docs/AddButton.png) 1. In the **Add search engine** form, paste the URL you copied (in step 1) in the **URL with %s in place of query** field. Insert the string `%s` into the URL's `?q=` query parameter (and remove any parts of the query that you don't want to keep). 2. Fill in the keyword field with the keyboard shortcut you want for searching on Sourcegraph. In this example, it's set to s. ![](https://storage.googleapis.com/sourcegraph-assets/Docs/Replace.png) 5.Click **ADD**. To use the shortcut, focus in the browser address bar, type in the keyword you chose (such as src), and press Tab. Now type your Sourcegraph code search query and hit enter. You'll be taken to the search results page on your Sourcegraph instance. ![](https://storage.googleapis.com/sourcegraph-assets/Docs/KeyboardShortcut.png) For more information on setting Chrome search engines, [see Chrome's help page](https://support.google.com/chrome/answer/95426?co=GENIE.Platform%3DDesktop&hl=en). ### Firefox To add Sourcegraph as a search engine in Firefox: 1. Visit https://sourcegraph.com/search (or your own Sourcegraph instance) 2. Click the three dots in the URL bar and choose **Add Search Engine**: ![image](https://storage.googleapis.com/sourcegraph-assets/Docs/84444295-4e2b8180-abf6-11ea-8f96-61312ab40278.png) 3. You may then search using your URL bar and select `Sourcegraph` from the list of search engines: ![image](https://user-images.githubusercontent.com/3173176/84446118-b4fe6a00-abf9-11ea-9627-7ffc16abafca.png) To configure a shortcut (keyword) for quicker access: 1. In the URL bar, enter about:preferences#search 1. Scroll down to **One-Click Search Engines**, double-click the **Keyword** cell for **Sourcegraph** and type in a shortcut (such as src). To use the search engine, type `src` `Tab` in the URL bar, then type your Sourcegraph search query. Pressing `Enter` will then go right to Sourcegraph! See "[Add or remove a search engine in Firefox](https://support.mozilla.org/en-US/kb/add-or-remove-search-engine-firefox)" for more information. ### Other web browsers Consult your web browser's documentation or settings page for information on how to add custom search engines. Most web browsers support this feature. # How to videos

Short, consumable videos to help you get started with Sourcegraph.

## [Code Search]()
# Cody ### Getting started with Cody | Topic | Description | | ---------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | | [Cody Web - Overview](https://www.loom.com/share/0989de19eafc44be9208f2ece9b2d901?sid=fa5965de-b844-406d-801a-bcf86f24913c?sid=bf56d3a2-e640-40ba-8cf0-d684ef9317f4) | Intro to the Cody web feature. | [Cody IDE - Overview](https://www.loom.com/share/44976666510f477da4d140273bd7615a?sid=bf56d3a2-e640-40ba-8cf0-d684ef9317f4) | Intro to the Cody extension in an IDE. | [Cody IDE - Chat](https://www.loom.com/share/a8c9403e10144f65a7413509c5f03f9c) | Intro to Cody chat in an IDE. | [Cody IDE - Autocomplete](https://www.loom.com/share/d1270097c6c148148bdbaf9a2f7bfe7e) | Intro to Cody autocomplete in an IDE. | [Cody IDE - Commands](https://www.loom.com/share/528b7b0ac36f4fbf8adb1cdba84f40da?sid=2d4e0354-5746-4beb-8848-bda547aa0110) | Intro to Cody commands in an IDE. ### Identifying Code AI Use Cases | Topic | Description | | ---------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | | [LLM Fundamentals](https://www.loom.com/share/379a3c6f9a2140cd8c5c8569ee7908e1?sid=c144e439-45a3-49fc-8034-8935fb23758e) | LLM basics and how they impact coding assistants. | [Cody Context Retrieval](https://www.loom.com/share/4bf1c37ccd8f425286618eaecce5d2ba?sid=629b5a84-2e48-4036-b89b-128a8155bdb3) | Demystifying Cody context retrieval. | [Task Categorization](https://www.loom.com/share/ab876ae8bed14ab889640e8f938d5ac7?sid=88c864e2-7fbb-4bc8-b213-5805eda9a1e5) | Introducing a task categorization framework to help you use coding assistants effectively. | [Cody Use Cases in the Developer Workflow](https://www.loom.com/share/d99d64ade19a447eaf512fa158d19092?sid=929c3811-4472-41c8-9568-20d10fb0efa5) | Thinking about how to use Cody in the typical developer workflow. # Code Search Full [search query syntax](/code-search/queries). ### Introduction | Topic | Description | | ---------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | | [Intro to Sourcegraph Search Types](https://www.loom.com/share/e5e97ea303ab408e99478f34052cfe1b?sid=49bd4515-d4d0-466b-be10-f40df13ce081) | Navigating the Code Search web app and the Keyword, Literal, Regex, and Structural search. | | [Search Results Page Overview](https://www.loom.com/share/c713baea045c40bdba9bc2f524ae4387?sid=8f6e3242-032c-40ec-9fa8-2006ad3c30f9) | Navigating the search results page. | | [Intro to Search Filters](https://www.loom.com/share/f48295092e7f4a9b874bfd8f81595a87?sid=dd05c740-be70-4374-b890-219a48c8d9f2) | An overview of the most commonly-used search filters such as Language, Repo, and File. | | [Search Types](https://www.loom.com/share/2484f5b47fec47c68369399a26a18150?sid=52b5df3f-35d4-4ea0-b009-cd64946d1d38) | Searching against Code, Repo, Path, Symbol, Commit, and Diff data. | | [Code Navigation](https://www.loom.com/share/a354ee1d87524239a8322d4c9c3131e9?sid=ce08dd70-4489-4cdc-b067-d470a505ce41) | Finding defintions and references, and an explanation of precise versus search-based [code navigation](/code-search/code-navigation). | | [Saving Searches](https://www.loom.com/share/5728c5fc9ac14a11aa9ca875c6eaa406?sid=eb6112d7-5f3e-45fb-ae89-a990355f0ab8) | [Saving](/code-search/working/saved_searches) commonly-used searches. | | [Search Contexts](https://www.loom.com/share/0b2ebb0a96154a87bad3c3c451f2ffcd?sid=dd211b02-6d7b-4c52-86fd-bebccc4a63d9) | Intro to [Search Contexts](/code-search/working/search_contexts.mdx) and how to manage them. | ### Advanced Searches | Topic | Description | | ---------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | | [Non-default branches, revisions, and tags](https://www.loom.com/share/2a812f39bc5548b19ff2e05e819485ec?sid=7bccac65-4354-4177-8dca-d22bff2480ff) | Intro to [Revision search](/code-search/queries/language#revision) and how to search for specific branches, revisions, and tags. | | [Commit messages](https://www.loom.com/share/2221265f062b4092bca6a69270fac84e?sid=b0d17c04-f01d-43dd-a690-a740b1bde4b0) | Searching `type:commit` messages. | | [Diffs](https://www.loom.com/share/daf8a5dcb5ea4ee0b41cddad7331f433?sid=a86b047f-f516-4c85-be1f-1dab5fd6d76d) | Searching `type:diff` . | | [Added or removed code](https://www.loom.com/share/ccb7ce60010749989978dd717faded8a?sid=7e610fe0-4914-4476-8596-2ef330701b10) | Combining `type:diff` with `select:commit.diff.(added OR removed)` . | | [By author](https://www.loom.com/share/5dfaa523713b4fa49526533abb0a9391?sid=6ca791e0-157b-486e-acb6-4584592c29c4) | Using the `author: ` filter. | | [Time-boxing searches](https://www.loom.com/share/104a5abfb3b74663b4b2f140d24bab3f?sid=5479b164-d6cb-4268-a959-00dbf01d8178) | Using the `before:` and `after:` filters. | | [Boolean operators](https://www.loom.com/share/0e762571a0334ceabf41c6bb4732642b?sid=34ecfad8-7a83-4c21-914b-3a90a0c3b9e2) | Using `AND`, `OR`, and `NOT` operators. | | [Functions and other symbols](https://www.loom.com/share/88755bf5490947a080e88a5fea040323?sid=9f815a7f-881f-4f0c-a55b-6e4b95699a0a) | Using `type:symbol` filter. | | [Advanced symbol search](https://www.loom.com/share/b8a1456c460442d3b42db1f2c783dd63?sid=a191fc76-34bf-4f71-89ee-6905235c8f47) | Combining the `select:symbol.(symbol_type)` and `type:symbol` filters. | | [Selecting metadata](https://www.loom.com/share/acb42f0611c14bb19dd24b28ac84ec2e?sid=2cf9ce6f-2862-4176-80b4-f66eb0e30277) | Using `select:` filter to return data other than the type of data searched against, e.g. returning all repos than contain a certain function. | | [Advanced repo search](https://www.loom.com/share/99e3f27c060b41d793e470a95c6f67bf?sid=8662effe-6e74-47c1-86aa-bef9e505a9a1) | Using [`repo:has.(...)`](/code-search/queries/language#built-in-repo-predicate) filters. | | [Excluding stale repos](https://www.loom.com/share/7c602971543546538c475a698e23871e?sid=ccd38787-eb5e-4e96-a7ac-255d699b8ed3) | Using `repo:has.commit.after(...)` filter. | ### Batch Changes and Code Insights | Topic | Description | | ---------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- | | [Batch Changes](https://www.loom.com/share/475d300b96c4404ba62edc8063499a05?sid=79e1fdb4-65ca-46d9-bfe9-d80257db831c) | Intro to managing [Batch Changes](/batch-changes) and the [batch spec](/batch-changes/batch-spec-yaml-reference) file. | | [Code Insights](https://www.loom.com/share/7066ac1ef33a41638793ac8f468b8ab8?sid=35b001df-84f8-4221-90c4-2ba46e03b9fc) | Intro to [Code Insights](/code_insights). | | [Code Monitoring](https://www.loom.com/share/99b8302e832a4341bc98916219fe8418?sid=261fc431-67e7-4df2-9cfa-306bcf003911) | Intro to [Code Monitoring](/code_monitoring). | # AWS Instance Sizing for Sourcegraph ## Instance size chart Select an instance type according to the number of users and repositories you have using this table. If you fall between two sizes, choose the larger of the two. For example, if you have 8,000 users with 80,000 repositories, your instance size would be **L**. If you have 1,000 users with 80,000 repositories, you should still go with size **M**. | | **XS** | **S** | **M** | **L** | **XL** | |----------------------|-----------------------|-----------------------|-----------------------|-----------------------|------------------------| | **Users** | ≥ 500 | ≥ 1,000 | ≥ 5,000 | ≥ 10,000 | ≥ 20,000 | | **Repositories** | ≥ 5,000 | ≥ 10,000 | ≥ 50,000 | ≥ 100,000 | ≥ 250,000 | | **Recommended Type** | m6a.2xlarge | m6a.4xlarge | m6a.8xlarge | m6a.12xlarge | m6a.24xlarge | | **Minimum Type** | m6a.2xlarge | m6a.2xlarge | m6a.4xlarge | m6a.8xlarge | m6a.12xlarge | While we recommend certain instance types in our sizing chart, the Sourcegraph AMI will dynamically use the resources available on the EC2 instance type it is deployed to, provided the minimum amount of resources needed is available. If you would like to resize your EC2 instance, follow the [upgrade steps](/admin/deploy/machine-images/aws-ami#upgrade) to switch to a more appropriate instance type. # Sourcegraph Tour Sourcegraph is a code search and intelligence tool for developers. It lets you search and explore your organization's code on the web, with integrations into your existing tools. Let's tour Sourcegraph to see how it helps with everyday developer tasks. ## Code review: Gain context on changed code A developer reviewing a pull request or diff might need to understand the changed code to see whether it'll have any negative impacts or if other necessary changes were accidentally omitted. Using Sourcegraph makes you a more effective code reviewer because code navigation makes it much easier to drill down into the implementation (to verify the correctness of the change) and enumerate call sites (to verify completeness of the change). ### Requirements - A [Sourcegraph Cloud](/cloud/) instance, or [deploy and configure Sourcegraph](/admin/deploy/) and add repositories - Install the integration for your code host/review tool ### Workflow 1. Visit a pull request or code review in your web browser. 2. Hover over source code in the diff (in added, unchanged, or deleted code) to see hover tooltips with documentation and **Go to definition** and **Find references** actions. 3. Use these actions while reviewing the diff, such as to ensure that all call sites of a modified function are updated to account for any new assumptions or behavior. To try this on an open-source pull request, install the browser extension and visit [gorilla/websocket#342](https://github.com/gorilla/websocket/pull/342/files). Use **Find references** to see all cases in which the newly added `AllowClientContextTakeover` is used. ## Example code: Learn how a particular function should be called A developer adding a new feature needs to know how I should call a particular function. Viewing cross-references (internal and external) on Sourcegraph is a great way to learn how to use a function or library because it shows you how it's _actually_ being used, not just what the (possibly outdated or incomplete) documentation might suggest. ### Requirements - [Deploy and configure Sourcegraph](/admin/deploy/) and add repositories - Install the browser extension and editor plugin for a faster way to initiate searches (optional) ### Workflow 1. Search on Sourcegraph for the name of the function you're trying to call. If you've installed any integrations, use those to initiate the search; otherwise, use the search box on the homepage of your organization's internal Sourcegraph instance. 1. Find and click on a search result for the function you're looking for. (If needed, narrow your search using the suggested search filters below the search box or by [adding your filters](/code-search/queries).) 1. Click on the function's name in the code file (if it's not already highlighted). 1. Click **Find references** to see how the function is called. 1. Click through to various function call sites and use the after-line blame's authorship and recency information to gauge the quality of the call site as an example. To try this on an open-source repository, start by searching for [repo:dgrijalva/jwt-go parsewithclaims](https://sourcegraph.com/search?q=repo:dgrijalva/jwt-go+parsewithclaims) and follow the steps above to view internal and external references for the [`ParseWithClaims`](https://sourcegraph.com/github.com/dgrijalva/jwt-go/-/blob/token.go#L92:6$references) function. ## Explore/read code: Understand what a function does and how it works As a new developer on a project, I need to understand the implementation details of a certain part of the code. Navigating your code with code navigation on Sourcegraph is a great way to understand code better because it works across all of your repositories (and all versions) without additional configuration, and it lets you share links with teammates if you need to ask specific questions about pieces of code. ### Requirements - [Deploy and configure Sourcegraph](/admin/deploy/) and add repositories - Install the browser extension and editor plugin for a faster way to initiate searches (optional) ### Workflow 1. Search on Sourcegraph to locate the part of the code you're interested in. If you've installed any integrations, use those to initiate the search; otherwise, use the search box on the homepage of your organization's internal Sourcegraph instance. 1. Find and click on a relevant search result or search suggestion. (If needed, narrow your search using the suggested search filters below the search box or by [adding your filters](/code-search/queries).) 1. Read through the code, click on a token, and then **Go to definition** to navigate to its definition as needed. 1. If you have unanswered questions, use the blame information to determine who wrote the code, and send them a Sourcegraph link to the relevant code along with your specific questions. Using an Open Source repo, you can follow the steps above to navigate to the implementation of the [`getLocation`](https://sourcegraph.com/github.com/Microsoft/node-jsonc-parser@e31983089c88114c7cc17f8c729feb493295c69d/-/blob/src/impl/parser.ts#L26:17) function. From there, keep drilling down depth-first until you get to the [`createScanner`](https://sourcegraph.com/github.com/Microsoft/node-jsonc-parser@e31983089c88114c7cc17f8c729feb493295c69d/-/blob/src/impl/scanner.ts#L13:17) function. Explicitly, this involves reading `getLocation` until you reach the first new function, `visit`; then finding the implementation of `visit` via "get references" and reading `visit` until you reach the first new function, `createScanner`. ## Debug issues: See what changed related to a function call that's erroring in production As a developer responsible for a service running in production, I need to respond to a crash report with a stack trace by quickly identifying and fixing the problem. Diff search on Sourcegraph is a great starting point for your investigation into what broke, because it shows you all recent changes that match your query. ### Requirements - [Deploy and configure Sourcegraph](/admin/deploy/) and add repositories - Install the browser extension and editor plugin for a faster way to initiate searches (optional) ### Workflow 1. Perform a diff search on Sourcegraph with the name of the function that the stack trace originates from, such as `type:diff myCrashingFunctionName`. If you've installed any integrations, use those to initiate the search; otherwise, use the search box on the homepage of your organization's internal Sourcegraph instance. 1. Scroll through the search results, which show you all commits (and diffs) that match the function name, newest first. (If needed, narrow the diff search by [adding search filters](/code-search/queries).) 1. Find and click on a relevant search result. On the search results page, clicking on the commit message brings you to the diff (with code navigation), and clicking on the code in the commit diff brings you to the full file at the revision before or after the commit. 1. Use **Go to definition** and **Find references** to understand the implementation changes and callers # Getting Started

This page will help you learn and understand about Sourcegraph and how to use it.

## What is Sourcegraph? Sourcegraph is a Code Intelligence platform that deeply understands your code, no matter how large or where it's hosted, to power modern developer experiences. ## Who should use Sourcegraph? In addition to the [companies listed on about.sourcegraph.com](https://about.sourcegraph.com), companies with a few hundred developers up to those with more than 40,000 use Sourcegraph daily. More specifically, Sourcegraph is great for all developers except: - those on smaller teams with a small amount of code - those who rarely search, read, or review code ## Why do I need Code Search? Facebook and Google provide their employees with an in-house Sourcegraph-like code search and intelligence tool. A [published research paper from Google](https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/43835.pdf) and a [Google developer survey](https://docs.google.com/document/d/1LQxLk4E3lrb3fIsVKlANu_pUjnILteoWMMNiJQmqNVU/edit#heading=h.xxziwxixfqq3) showed that 98% of developers surveyed consider their Sourcegraph-like internal tool to be critical. Developers use it on average for 5.3 sessions each day. (Facebook's and Google's in-house tools are unavailable to other companies; use Sourcegraph instead.) ## What do I use Sourcegraph for? Sourcegraph helps you: - Find example code - Explore/read code (including during a code review) - Debug issues - Locate a specific piece of code - Determine the impact of changes - And more! Sourcegraph makes it easier for you and everyone else in your organization to perform these tasks. ## What does Sourcegraph do? Sourcegraph's main features are: - [Code navigation](#code-navigation): jump-to-definition, find references, and other smart, IDE-like code browsing features on any branch, commit, or PR/code review - [Code search](#code-search): fast, up-to-date, and scalable, with regexp support on any branch or commit without an indexing delay (and diff search) - [Notebooks](#notebooks): pair code and markdown to create powerful live and persistent documentation - [Cody](#cody): read and write code with the help of a context-aware AI code assistant - [Code Insights](#code-insights): reveal high-level information about your codebase at its current state and over time to track migrations, version usage, vulnerability remediation, ownership, and anything else you can search in Sourcegraph - [Batch Changes](#batch-changes): make large-scale code changes across many repositories and code hosts - [Integrations](#integrations) with code hosts, code review tools, editors, web browsers, etc. ## How do I start using Sourcegraph? 1. [Deploy and Configure Sourcegraph](/admin/deploy/) inside your organization on your internal code if nobody else has yet 1. Install and configure the [web browser code host integrations](/integration/browser_extension) (recommended) 1. Start searching and browsing code on Sourcegraph by visiting the URL of your organization's internal Sourcegraph instance 1. [Personalize Sourcegraph](/getting-started/personalization/) with themes, quick links, and badges! You can also try [Sourcegraph.com](https://sourcegraph.com/search), a public Sourcegraph instance for use on open-source code only. ## How is Sourcegraph licensed? Sourcegraph Enterprise is Sourcegraph's primary offering and includes all code intelligence platform features. Sourcegraph Enterprise is the best solution for enterprises who want to use Sourcegraph with their organization's code. ## How is Sourcegraph different from the GitHub code search? - [See how GitHub code search compares to Sourcegraph](/getting-started/github-vs-sourcegraph) ## Code Search Sourcegraph code search is fast, works across all your repositories at any commit, and has minimal indexing delay. Code search also includes advanced features, including: - [Powerful, flexible query syntax](/code-search/queries) - [Commit diff search](/code-search/features#commit-diff-search) - [Commit message search](/code-search/features#commit-message-search) - [Saved search scopes](/code-search/features#search-scopes) - [Search contexts to search across a set of repositories at specific revisions](/code-search/features#search-contexts) - [Saved search monitoring](/code_monitoring/) Read the [code search documentation](/code-search/) to learn more and discover the complete feature set. Here's a video to help you get started: - [How to Search commits and diffs with Sourcegraph](https://youtu.be/w-RrDz9hyGI) - [Search Examples](https://sourcegraph.github.io/sourcegraph-search-examples/) ## Code Navigation Sourcegraph gives your development team cross-repository IDE-like features on your code: - Hover tooltips - Go-to-definition - Find references - Symbol search Sourcegraph gives you code navigation in: - **code files in Sourcegraph's web UI** ![Hover tooltip](https://storage.googleapis.com/sourcegraph-assets/code-graph/docs/hover-tooltip.png) - **diffs in your code review tool**, via [integrations](/integration/) ![GitHub pull request integration](https://storage.googleapis.com/sourcegraph-assets/code-graph/docs/github-pr.png) - **code files on your code host**, via [integrations](/integration/) ![GitHub file integration](https://storage.googleapis.com/sourcegraph-assets/code-graph/docs/github-file.png) Please read the [code navigation documentation](/code-search/code-navigation/) to learn more and to set it up. ## Cody Cody is an AI code assistant that uses Sourcegraph code search, the code graph, and LLMs to provide context-aware answers about your codebase. Cody can explain code, refactor code, and write code, all within the context of your existing codebase. [Learn more about Cody](/cody/overview/). ## Notebooks GA in version 3.39 or later Increase dev collaboration and self-service through live documentation covering best practices, shared code, and processes. Read the [Notebooks documentation](/notebooks/) to learn more, and check out these [publicly available notebooks](https://sourcegraph.com/notebooks?_ga=2.9922293.1906238367.1667257632-528424761.1615652680&_gl=1*1xalma3*_ga*NTI4NDI0NzYxLjE2MTU2NTI2ODA.*_ga_E82CCDYYS1*MTY2NzI1NzYzMi41NS4xLjE2NjcyNjA3NjIuMC4wLjA.). ## Batch Changes Batch Changes is an Enterprise feature. Automate changes to your codebase. Reduce effort, reduce errors, and enable developers to focus on high-value work. Read the [batch changes documentation](/batch-changes/) to learn more, including helpful how-to guides. Want a video tutorial? Check out this [batch change tutorial](https://www.youtube.com/watch?v=eOmiyXIWTCw) ## Code Insights Code Insights is an Enterprise feature. Sourcegraph lets you understand and analyze code trends by visualizing how the codebase changes. Measure and act on engineering goals such as migration and component deprecation. Read the [code insights documentation](/code_insights/) to learn more, including helpful how-to guides. ## Integrations Sourcegraph allows you to get code navigation and code search on code files and code review diffs in your code host and review tool or from your IDE. ### IDE integration Sourcegraph's editor integrations allow you to search and navigate across all of your repositories without ever leaving your IDE or checking them out locally. Learn more about how to set them up [here](/integration/editor). ### Browser extension Our browser extension directly adds code navigation within your code hosts (GitHub, GitLab, Bitbucket, and Phabricator) via Chrome, Safari, and Firefox browsers. Learn more about how to set up the browser extension [here](/integration/browser_extension/).
# GitHub code search vs. Sourcegraph GitHub code search is the next iteration of GitHub’s native code search and navigation functionality that can be used to search code stored in GitHub. It’s currently in beta preview. Sourcegraph is a code intelligence platform that makes codebases intelligible by semantically indexing and analyzing all of an organization’s code, providing developers and engineering leaders with a complete understanding of their codebase. In addition to universal code search across every code host, Sourcegraph has features to help developers find code, understand and answer questions about code, and fix code faster. ## Which is best for you? **Who should use GitHub code search?** If you’re brand new to code search and you simply want to try it out, start with GitHub code search across your own code. If you have a small codebase hosted exclusively in GitHub, GitHub's native code search will likely be sufficient as you get started with code search. **Who should use Sourcegraph?** As your codebase grows in complexity, the value of code search quickly increases. Sourcegraph may be a good fit for your team if: * You have a large number of repositories or a large and complex monorepo * You host code across multiple code hosts (or you don’t have any code in GitHub) If you frequently rely on your editor’s “go to definition” and “find references” features, you’ll also be able to take advantage of Sourcegraph's precise code navigation. Only Sourcegraph's offering features IDE-level accuracy and works across repositories. If you're brand new to code search and you want to try it, visit [Sourcegraph.com](https://sourcegraph.com/search) to search across open source repositories. For a high-level overview of how Sourcegraph compares to GitHub code search, see this [document](https://storage.googleapis.com/sourcegraph-assets/docs/PDFs/Sourcegraph%20vs.%20GitHub%20code%20search%20chart.pdf). ## Searching code ### Code host integrations GitHub code search can only be used to search across code that is stored in GitHub. Organizations with code stored in multiple code hosts cannot use GitHub code search to search across their entire codebase. Sourcegraph integrates with multiple code hosts to provide universal code search across all of your organization’s code, no matter where it’s stored. Sourcegraph has out-of-the-box [integrations](/integration) with: * GitHub * GitLab * Bitbucket Cloud and Bitbucket Server / Bitbucket Data Center * Perforce You can also integrate Sourcegraph with other Git-based code hosts using [these](/admin/code_hosts/other) instructions or use the [Sourcegraph CLI (src)](/admin/code_hosts/src_serve_git) to load local repositories without a code host into Sourcegraph. Sourcegraph is continuously adding [Tier 1](/admin/code_hosts) support for additional code hosts.
GitHub Sourcegraph
GitHub
GitLab
Bitbucket Cloud
Bitbucket Server / Bitbucket Data Center
Perforce
Any Git-based code host
### Searching repositories, branches, and forks GitHub allows you to search indexed code, but not all code is indexed. GitHub’s current limitations on indexed code are: * Files over 350 KiB and empty files are excluded * Only UTF-8 encoded files are included * All code inside very large repositories may not be indexed * Vendored and generated code is excluded (as determined by [Enry](https://github.com/go-enry/go-enry)) With GitHub, only the default **branch** is searchable (though GitHub is planning to support branch search in the future). **Forks** are included in the index but are subject to the same limitations as other repositories, so not all forks are indexed. You may need to include the fork filter to retrieve results for the fork repos, but an admin can adjust global settings to include forks in search query results automatically. GitHub code search supports searching across issues, pull requests, and discussions. In addition to searching your private code, GitHub has indexed over 7 million public GitHub repositories which are also searchable. Sourcegraph allows you to search both indexed and unindexed code. Sourcegraph’s [current limitations](/admin/search) on indexed code are: * Files larger than 1 MB are excluded unless you explicitly specify them in [search.largeFiles](/admin/config/site_config#search-largeFile) to be indexed and searched regardless of size * Binary files are excluded * Files other than UTF-8 are excluded With Sourcegraph, typically, the latest code on the default **branch** of each repository is indexed (usually the master or main), but Sourcegraph can also index other non-default branches, such as long-running branches like release branches. If you’re searching outside of indexed branches, you can use unindexed search. You should expect slightly slower results when searching unindexed code. In addition to searching your organization’s private code, you can use Sourcegraph.com to search across 2.8 million public repositories from multiple code hosts. | **Features** | **GitHub** | **Sourcegraph** | | ---------------------------------------------------- | ----------------------- | --------------------------------------- | | Search across all repositories and forks | ✓ with limitations | ✓ with limitations | | Search across files larger than 350 KiB | ✗ | ✓ (Using the search.largeFiles keyword) | | Search across all branches | Only the default branch | ✓ | | Number of open source repositories indexed | 7 million | 2.8 million | | Search across issues, pull requests, and discussions | ✓ | ✗ | ### Search syntax Sourcegraph offers [structural search](/code-search/types/structural), and GitHub code search does not offer this search method. Structural search lets you match richer syntax patterns, specifically in code and structured data formats like JSON. Sourcegraph offers structural search on indexed code and uses [Comby syntax](https://comby.dev/docs/syntax-reference) for structural matching of code blocks or nested expressions. For example, the `fmt.Sprintf` function is a popular print function in Go. [Here](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/about%24+fmt.Sprintf%28...%29&patternType=structural&_ga=2.204781593.827352295.1667227568-1057140468.1661198534&_gac=1.118615675.1665776224.CjwKCAjwkaSaBhA4EiwALBgQaJCOc6GlhIDQyg6HQScgfSBQpoFTUf7T_NNqEX5JaobtCS08GUEJuRoCIlIQAvD_BwE&_gl=1*1r2u5zs*_ga*MTA1NzE0MDQ2OC4xNjYxMTk4NTM0*_ga_E82CCDYYS1*MTY2NzUwODExNC4xMTQuMS4xNjY3NTA5NjUyLjAuMC4w) is a pattern that matches all of the arguments in `fmt.Sprintf` in our code using structural search compared to the [search](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/about%24+fmt.Sprintf%28...%29&patternType=regexp) using regex. Both GitHub code search and Sourcegraph support regular expression and keyword search. [Regular expression](/code-search/queries#standard-search) helps you find code that matches a pattern (including classes of characters like letters, numbers, and whitespace) and can restrict the results to anchors like the start of a line, the end of a line, or word boundary. Keyword search matches on individual terms, supporting both literal searches and flexible keyword-style queries. GitHub’s search syntax can be found [here](https://cs.github.com/about/syntax), and Sourcegraph’s search syntax can be found [here](/code-search/queries). Regardless of the type of search method you use, GitHub’s search is line-oriented, and Sourcegraph supports multi-line search. This means that Sourcegraph’s search queries can find results that cross multiple lines. For example, here is an [example](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/Parsely/pykafka%24+Not+leader+for+partition&patternType=regexp&_ga=2.114069518.827352295.1667227568-1057140468.1661198534&_gac=1.47310293.1665776224.CjwKCAjwkaSaBhA4EiwALBgQaJCOc6GlhIDQyg6HQScgfSBQpoFTUf7T_NNqEX5JaobtCS08GUEJuRoCIlIQAvD_BwE&_gl=1*zwylx9*_ga*MTA1NzE0MDQ2OC4xNjYxMTk4NTM0*_ga_E82CCDYYS1*MTY2NzU3NDA3OC4xMTcuMS4xNjY3NTc2NjIyLjAuMC4w) of matching multiple text strings in a file using regex, and here is a second explicit multi-line search [example](https://sourcegraph.com/search?q=context:global+app.terraform.io/example_corp+%5Cn+version+%3D%28.*%290.9.%5Cd+lang:Terraform&patternType=regexp) for terraform module verisions. ### Commit diff search and commit message search Commit diff and commit message searches help you see how your codebase has changed over time. With Sourcegraph, you can search within [commit diffs](/code-search/features#commit-diff-search) to find changes to particular functions, classes, or specific areas of the codebase, and you can search over [commit messages](/code-search/features#commit-message-search) and narrow down searches with additional filters such as author or time. GitHub code search does not offer commit diff search or commit message search. ### Symbol search Symbol search makes it easier to find specific functions, variables, and more by filtering searches for symbol results. Symbol results also allow you to jump directly to symbols by name. Both GitHub code search and Sourcegraph support symbol searching. GitHub supports symbol search in 10 languages, including C#, Python, Go, Java, JavaScript, TypeScript, PHP, Protocol Buffers, Ruby, and Rust. Sourcegraph’s [symbol search](/code_navigation/explanations/features#symbol-search) is available for more than [75 languages](https://about.sourcegraph.com/blog/introducing-sourcegraph-server-2-6). | **Features** | **GitHub** | **Sourcegraph** | | --------------------------------------- | -------------------------------------------------------------------------------------------------- | --------------- | | Structural search | ✗ | ✓ | | Keyword search | ✓ | ✓ | | Regular expression search | ✓ | ✓ | | Multi-line search support | ✗ | ✓ | | Commit diff and commit message searches | ✗ | ✓ | | Symbol search | 10 languages (C#, Python, Go, Java, JavaScript, TypeScript, PHP, Protocol Buffers, Ruby, and Rust) | 75+ languages | ### Search results and result types **Search results** GitHub only returns the first 10 pages of search results. You cannot currently go past the 10th page or retrieve all search results. Sourcegraph can retrieve all search results. By default, Sourcegraph returns 500 search results, but this number can be increased by increasing the ‘count’ value. Sourcegraph can display a maximum of 1,500 results, but all matches can be fetched using the [src CLI](/cli/quickstart), the [Stream API](/api/stream_api), or [GraphQL API](/api/graphql). You can also export the results via CSV. GitHub code search includes suggestions, completions, and the ability to save your searches. Sourcegraph offers suggestions through search query examples and [saved searches](/code-search/working/saved_searches#creating-saved-searches). GitHub code search returns a list of repositories and files. Sourcegraph results can include repositories, files, diffs, commits, and symbols; however, you must use the ‘type’ filter to return anything outside of repositories and files. **Ranking** Both GitHub and Sourcegraph display the most relevant search results first using a variety of heuristics. GitHub analyzes how many matches are in the file, the quality of the matches, the kind of file, whether the searches match symbols, and the number of repository stars as inputs for ranking results. Sourcegraph uses a repository’s number of stars to [rank](https://docs-legacy.sourcegraph.com/dev/background-information/architecture/indexed-ranking) the most important repositories first There are several other heuristic signals that help to make sure that the most important documents are searched first, including: * Up rank files with a lot of symbols * Up rank small files * Up rank short names * Up rank branch count * Down rank generated code * Down rank vendored code * Down rank test code When submitting a search query, the quality of a match is scored based on language-specific heuristics (Java classes rank higher than variables), word boundaries, and symbol ranges. The score that is received at the time of the query is combined with the index time rank and the repository’s priority to determine the final ranking. [Sourcegraph.com](https://sourcegraph.com/search) (Sourcegraph's public instance for searching open source code) utilizes an algorithm inspired by Google PageRank to measure code reuse and return the most relevant search results first. This new ranking algorithm will be implemented for Sourcegraph customer instances (self-hosted and Cloud) in the future. | **Features** | **GitHub** | **Sourcegraph** | | ----------------------------------------------------------------------------------------- | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Comprehensive search results | ✗ (Limited to 10 pages of results) | ✓ (500 search results are returned by default, but this number can be increased by increasing the ‘count’ value. A maximum of 1,500 results can be displayed, and more matches can be fetched using the src CLI, the Stream API, or GraphQL API.) | | Ranking based on a variety of heuristics including usage of code, files, and repositories | ✓ | ✓ | ### Search aggregations To help you understand and refine search results, Sourcegraph’s search results also include visual search aggregation charts. These charts help you answer unique questions about the overall results set that individual search results cannot, like how many different versions of a library or package are present in your code, which repositories in a given library are most used, and more. They can also help you quickly refine your search by seeing which files, repositories, authors, or capture group returned the most results, and then clicking a result in the chart to add a filter to your query. You can group your search results by location (repository or file), author, or arbitrary capture group pattern. Example search aggregations can be found [here](/code_insights/references/search_aggregations_use_cases). GitHub code search does not currently offer this functionality. ### Search filters and contexts **Filters** Code search filters help you refine and narrow search query results to be more relevant. Both GitHub code search and Sourcegraph include filters. GitHub code search includes filters such as language, repository, path, and file size. GitHub automatically suggests filters to apply to your search based on your search history and information about you, such as your organization. GitHub also offers auto-complete using filters to complete a code search query. Sourcegraph filters reduce the scope of search query results by language, repository, path, author, message, content, timeframe, visibility, and more. Sourcegraph offers auto-completion on filters in the search query. **Search contexts** Search contexts are an alternative way to narrow down the scope of your search to the code you care about. Search contexts are available with both GitHub and Sourcegraph. You can create custom search contexts to be simple or advanced with GitHub. Simple search contexts are things such as repository or organization name, and advanced search contexts can mix multiple attributes, including languages. GitHub’s search contexts allow for more personalization than Sourcegraph. With Sourcegraph, a [search context](/code-search/working/search_contexts) represents the body of code that will be searched. Search contexts can be private to the user who creates it or shared with other users on the same Sourcegraph instance. [Query-based ](/code-search/working/search_contexts#beta-query-based-search-contexts)search contexts (beta) are an additional way to create search contexts based on variables like repository, rev, file, lang, case, fork, and visibility. Both [OR] and [AND] expressions are allowed to help further narrow the scope of query-based search contexts. | **Features** | **GitHub** | **Sourcegraph** | | --------------- | ----------------------- | ------------------------------------ | | Filters | ✓ | ✓ | | Search contexts | ✓ (Simple and advanced) | ✓ (Repository-based and query-based) | ## Understanding and navigating code **Code navigation** helps you explore code in depth. It includes features such as “Go to definition” and “Find references,” which let you quickly move between files to understand code. ### Search-based code navigation Both GitHub and Sourcegraph offer out-of-the-box, search-based code navigation. This version of code navigation uses search-based heuristics to find references and definitions across a codebase without any setup required. It is powerful and convenient, but it can sometimes present inaccurate results. For example, it can return false positive references for symbols with common names. It is limited to returning definitions and references within a single repository (it won’t track references across multiple repositories). GitHub’s [search-based code navigation](https://docs.github.com/en/repositories/working-with-files/using-files/navigating-code-on-github) officially supports 10 languages according to the documentation, but more languages are supported in the latest beta preview. Sourcegraph’s [search-based code navigation](/code_navigation/explanations/search_based_code_navigation) supports 40 languages. | **Features** | **GitHub** | **Sourcegraph** | | ------------------------ | ------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Technical implementation | Heuristic-based | Heuristic-based | | Language support | 10 languages (C#, CodeQL, Elixir, Go, Java, JavaScript,TypeScript, PHP, Python, Ruby) | 40 languages (The full list of supported languages can be found [here](/code_navigation/explanations/search_based_code_navigation)) | | Setup | No setup required | No setup required | | Accuracy | Moderate (some false positives) | Moderate (some false positives) | | Cross-repository | ✗ | ✗ | ### Precise code navigation GitHub and Sourcegraph both offer precise code navigation as well. Despite having the same name, the two versions of precise code navigation are very different in terms of the underlying technology and accuracy they provide. Only Sourcegraph’s precise code navigation is 100% accurate. GitHub’s [precise code navigation](https://docs.github.com/en/repositories/working-with-files/using-files/navigating-code-on-github#precise-and-search-based-navigation) is an improved form of heuristic-based code navigation which uses syntax trees to offer higher accuracy for references and definitions and cross-repository navigation. It is more accurate than GitHub’s search-based code navigation, but it can still present inaccuracies. It is available out-of-the-box on GitHub and is automatically used over search-based code navigation when available. It is supported for 1 language, Python. Sourcegraph’s [precise code navigation](/code_navigation/explanations/precise_code_navigation) is not heuristic-based. Instead, it uses [SCIP and LSIF](/code_navigation/references/indexers) data to deliver precomputed code navigation, meaning that it is fast and compiler-accurate. It is the only 100% accurate solution for code navigation between Sourcegraph’s and GitHub’s offerings. Because precise code navigation uses code graph (SCIP) data, it is not susceptible to false positives or other potential errors (such as those caused by symbols with the same name). It also supports cross-repository navigation, which shows symbol usage across repositories and [transitive dependencies](/code_navigation/explanations/features#beta-dependency-navigation). It also has a unique feature, “Find implementations,” which allows you to navigate to a symbol’s interface definition or find all the places an interface is being implemented. Sourcegraph’s precise code navigation is opt-in and requires you to upload code graph data ([LSIF or SCIP](/code_navigation/references/indexers)) to Sourcegraph. This data can be automatically generated and uploaded to Sourcegraph via [auto-indexing](/code_navigation/explanations/auto_indexing). For repositories without SCIP or LSIF data, Sourcegraph automatically falls back to search-based code navigation. Sourcegraph’s precise code navigation [supports 11 languages](/code_navigation/references/indexers). | **Features** | **GitHub** | **Sourcegraph** | | ------------------------ | --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- | | Technical implementation | Heuristic-based | SCIP-based (code graph data) | | Accuracy | High (some false positives) | Perfect (compiler-accurate) | | Language support | 1 language (Python) | 11 languages (Go, TypeScript, JavaScript, C, C++, Java, Scala, Kotlin, Rust, Python, Ruby) | | Setup | No setup required | Opt-in (Must set up LSIF/SCIP indexing. [Auto-indexing](/code_navigation/explanations/auto_indexing) available.) | | Cross-repository | ✓ | ✓ | ## Codebase insights and analytics GitHub and Sourcegraph have offerings that are called “insights,” but they differ. Generally, insights can be split into two categories: ### Codebase insights GitHub does not offer comprehensive insights that account for the content of the code itself. Rather, GitHub’s insights are based primarily on GitHub’s product-level data. GitHub offers [dependency insights](https://docs.github.com/en/enterprise-cloud@latest/admin/policies/enforcing-policies-for-your-enterprise/enforcing-policies-for-dependency-insights-in-your-enterprise) that show all the packages your organization’s repositories depend on, e.g. aggregated information about security advisories and licenses. [Sourcegraph’s Code Insights](/code_insights) is based on codebase-level data: aggregation of lines, patterns, and other search targets in the codebase. It reveals high-level information about the entire codebase, accounting for all of your repositories and code hosts. With Code Insights, you can track anything that can be expressed with a Sourcegraph search query and turn it into customizable dashboards. Code Insights can be used to track migrations, package use, version adoption, code smells, vulnerability remediation, codebase size, and more. ### App activity insights With [insights for Projects](https://docs.github.com/en/issues/planning-and-tracking-with-projects/viewing-insights-from-your-project/about-insights-for-projects) in GitHub, you can view, create, and customize charts that are built from the project’s data. [Organization activity insights](https://docs.github.com/en/enterprise-cloud@latest/organizations/collaborating-with-groups-in-organizations/viewing-insights-for-your-organization) help you understand how members of the organization are using GitHub, e.g. issue and pull request activity, top languages used, and cumulative information about where members spend their time. Sourcegraph offers [in-product analytics](/admin/analytics) to help Sourcegraph administrators understand user engagement across the various Sourcegraph features, identify power users, and convey value to engineering leaders. | **Features** | **GitHub** | **Sourcegraph** | | ------------------------------------------ | -------------------------------------------------------------------------------------------------- | --------------- | | Codebase insights | ✗ (Offers dependency insights. Does not offer customizable insights about the content of the code) | ✓ | | App activity and project activity insights | ✓ | ✓ | ## Large-scale code changes GitHub does not offer a way to automate arbitrary large-scale code changes. For repositories where [Dependabot security updates](https://docs.github.com/en/enterprise-cloud@latest/code-security/dependabot/dependabot-security-updates/about-dependabot-security-updates) are enabled, when GitHub Enterprise Cloud detects a vulnerable dependency in the default branch, Dependabot creates a pull request to fix it. With Sourcegraph’s [Batch Changes](/batch-changes), you can make any large-scale code change across many repositories and code hosts. You can create pull requests on all affected repositories and [track the progress](/batch-changes/tracking-existing-changesets) until they are all merged. You can also preview the changes and update them at any time. Batch Changes is often used to make the following types of changes: updating API callsites after a library upgrade, updating configuration files, changing boilerplate code, renaming symbols, patching critical security issues, and more. | **Features** | **GitHub** | **Sourcegraph** | | ------------------------ | ------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Large-scale code changes | [Automatic Dependabot updates](https://docs.github.com/en/enterprise-cloud@latest/code-security/dependabot/dependabot-security-updates/about-dependabot-security-updates) are possible, but there is no support for applying arbitrary large-scale changes | You can apply arbitrary code changes across many repositories and code hosts by running any codemod tool, using a template, or writing your own; You can manage and track the resulting PRs until they are merged | | Completeness | Dependency upgrades only; in GitHub only | Any code change across multiple code hosts | ## Integrations and API ### Integrations Both GitHub and Sourcegraph offer integrations to help optimize your workflow. GitHub’s owned integrations are built and managed by GitHub, and they have a marketplace with nearly a thousand third-party applications spanning across categories such as code quality, code review, IDEs, monitoring, security, and more. These integrations are available for GitHub overall, but there aren’t any integrations related to GitHub code search. For example, the [VS Code integration](https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github) allows you to review and manage pull requests, but it does not let you use GitHub code search in VS Code. Sourcegraph’s [editor integrations](/integration/editor) let you search and navigate across all of your repositories from all your code hosts and across all GitHub instances and organizations without leaving your IDE. Sourcegraph currently integrates with VS Code, JetBrains IDEs, and Gitpod. You can also add Sourcegraph to your preferred [browser](/integration/browser_extension/how-tos/browser_search_engine) to quickly search across your entire codebase from within your browser. ### API GitHub has a REST API for web clients, but it is not yet documented. On the other hand, Sourcegraph offers different APIs that help you access code-related data available on a Sourcegraph instance. The [GraphQL API](/api/graphql) accesses data stored and computed by Sourcegraph. This API can [fetch](/api/graphql/examples) file contents without cloning a repository or search for a new API and determine all of the repositories that haven’t migrated to it yet. The [Stream API](/api/stream_api) supports consuming search results as a stream of events and it can be used to [search](/api/stream_api#example-curl) over all indexed repositories. Lastly, use the [interactive API explorer](https://sourcegraph.com/api/console#%7B%22query%22%3A%22%23%20Type%20queries%20here%2C%20with%20completion%2C%20validation%2C%20and%20hovers.%5Cn%23%5Cn%23%20Here's%20an%20example%20query%20to%20get%20you%20started%3A%5Cn%5Cnquery%20%7B%5Cn%20%20currentUser%20%7B%5Cn%20%20%20%20username%5Cn%20%20%7D%5Cn%20%20repositories%28first%3A%201%29%20%7B%5Cn%20%20%20%20nodes%20%7B%5Cn%20%20%20%20%20%20name%5Cn%20%20%20%20%7D%5Cn%20%20%7D%5Cn%7D%5Cn%22%7D) to build and test your API queries. | **Features** | **GitHub** | **Sourcegraph** | |---------------------------------------------|-----------------------------------------------------------|---------------------------------------------------------------| | Code search integrations and extensions | ✗ (GitHub built and marketplace with integrations, but no code search integration or extensions) | ✓ (Editor integrations and browser extensions) | | API | ✗ | ✓ (GraphQL API and Stream API) | ## Alerting GitHub and Sourcegraph offer alerting on code in different forms. GitHub offers [code scanning](https://docs.github.com/en/enterprise-cloud@latest/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/setting-up-code-scanning-for-a-repository), which allows you to set up CodeQL or third-party analysis to run over code and generate alerts. CodeQL is a flexible code analysis tool used to query code and return arbitrary data. Code scanning is applied at the repository level and can be used to alert on both merged code and pull requests. Individuals can then personally configure notifications for when code scanning workflows are completed on a given repository. Notifications can go through email, the GitHub web interface, or GitHub Mobile. GitHub also offers [Dependabot alerts](https://docs.github.com/en/enterprise-cloud@latest/code-security/dependabot/dependabot-alerts/about-dependabot-alerts). Dependabot detects insecure dependencies of repositories and triggers alerts when a new advisory is added to the [GitHub Advisory Database](https://docs.github.com/en/enterprise-cloud@latest/code-security/dependabot/dependabot-alerts/browsing-security-advisories-in-the-github-advisory-database) or the [dependency graph](https://docs.github.com/en/enterprise-cloud@latest/code-security/supply-chain-security/understanding-your-software-supply-chain/about-the-dependency-graph) for a repository changes. GitHub can also review and alert on dependency changes in pull requests made against the default branch of a repository. These alerts are viewable by admins in each repository, and admins can make them viewable to other users as well. These alerts can be sent to you via several channels: email, web interface, command line, and/or GitHub Mobile. Sourcegraph offers [code monitors](/code_monitoring), which continuously monitor the results of a specific search query and generate alerts when new results are returned. Code monitors only look at merged code, and they can be used to trigger alerts when undesirable code is added to a codebase. This can include known vulnerabilities, bad patterns, file changes, or consumption of deprecated endpoints (anything that can be queried via Sourcegraph). Each code monitor can span any scope of your choosing, such as a single repository, multiple repositories, or multiple code hosts. They can also be scoped to specific branches of a repository. Code monitor alerts can be configured to send notifications via email, Slack message, or webhook. Most queries used for a code monitor can also be reused for a Code Insights chart or a fix with Batch Changes. ## Embedded code search and documentation Sourcegaph’s [Notebooks](/notebooks) integrate code search with Markdown for knowledge sharing. Notebooks are created with blocks, and each block can be Markdown, a code search query, a live code snippet, a file, or a symbol. Notebooks pull information directly from your codebase, so the information served in notebooks (via code search blocks, for example) always reflects what is live in your code at that moment in time. By referencing live code, notebooks are useful for onboarding teammates, documenting vulnerabilities, walking through complex parts of a codebase, or keeping track of useful queries. GitHub does not currently offer functionality to embed code search within notebooks or documentation. ## Licensing GitHub is closed source, while Sourcegraph’s code is publicly available. ## Availability GitHub code search is currently available through a beta preview (you can sign up for their [waitlist](https://github.com/features/code-search-code-view/signup) to request access). The beta preview is not yet available for GitHub Enterprise. Sourcegraph’s code intelligence platform is generally available, and you can sign up for a free trial for your team [here](https://about.sourcegraph.com/get-started?t=enterprise/).
# Welcome to Sourcegraph This is a compilation of the most important information you will need to get started. ## Sync Code Hosts Sourcegraph is nothing without your code, so connecting your code hosts is crucial. How to connect to… - [GitHub](/admin/code_hosts/github) - [GitLab](/admin/code_hosts/gitlab) - [Bitbucket Cloud](/admin/code_hosts/bitbucket_cloud) - [Bitbucket Server / Bitbucket Data Center](/admin/code_hosts/bitbucket_server) - [Other Git code hosts (using a Git URL)](/admin/code_hosts/other) - [Non git hosts](/admin/code_hosts) ## Inviting users and SSO A team is essential when using Sourcegraph. Be sure to spread the word and get everybody in. ### Setting up SSO Sourcegraph supports out-of-the-box support for different auth providers. Our documentation provides guidance on how to setup these. - [User authentication (SSO) - Sourcegraph docs](/admin/auth) You may not have the right privileges or role in your team to set this up. In that case, you could invite someone with the right privileges and make them a site-admin. ### Inviting Single Users Getting users into Sourcegraph is easy, you just need to navigate to: **Site Admin** → **Users and auth** → **Users** → **+ Create user** ## Setup Email Server By default the Sourcegraph instance cannot send emails. So features like password resets, email invites and email code monitors will not work. - [Configure email sending / SMTP server](/admin/config/email) ## Learn Sourcegraph Extra resources that we think are helpful: - [Sourcegraph 101](/getting-started) - [Search Examples](/code-search/queries/examples) ## Community and Support [Join our discord server](https://srcgr.ph/discord-cloud-onboarding). If you need support, reach to via `support@sourcegraph.com`. # Color themes Sourcegraph has two color themes: - Light theme - Dark theme Signed-in users can switch between the color themes by going to **User menu** > **Use dark/light theme** . # Personalizing Sourcegraph - [Light and dark themes](/getting-started/personalization/themes) - [Badges for Go public repositories](/getting-started/personalization/badges) # Repository badges (Go open-source projects only) Right now, this feature is only supported for open-source Go repositories on Sourcegraph.com. Sourcegraph.com generates badges with usage statistics for open-source Go repositories. The badges shows the total number of Go packages (among all known public repositories) that import the repository's Go packages. To add a badge to your project's README, add this Markdown: ``` markdown [![Sourcegraph](https://sourcegraph.com/github.com/gorilla/mux/-/badge.svg)](https://sourcegraph.com/github.com/gorilla/mux?badge) ``` (Be sure to replace `github.com/gorilla/mux` with your own repository's name.) This renders the following badge: [![Sourcegraph](https://sourcegraph.com/github.com/gorilla/mux/-/badge.svg)](https://sourcegraph.com/github.com/gorilla/mux?badge) ## Known issues Please report any other issues and feature requests [here](https://help.sourcegraph.com/hc/en-us/requests/new). - The number may be overcounted because it is the sum of counts for all of the repository's subpackages. If another project uses multiple subpackages in this repository, the project is counted multiple times. - Importers using custom Go import paths (i.e., anything other than import paths prefixed by the repository name, such as `github.com/foo/bar`) will not be counted. # Indexing the OSS universe Sourcegraph is on a mission to [make coding more accessible to the world](https://handbook.sourcegraph.com/company/strategy). In order to execute this mission, Sourcegraph has committed to indexing open source repositories from various code hosts including GitLab and GitHub. By indexing these repositories, Sourcegraph allows any engineer to tap into the knowledge of the open source universe near-instantaneously. You can read more about our [decision to do this](https://about.sourcegraph.com/blog/indexing-the-oss-universe-update-more-code-hosts/). Sourcegraph's open source code index includes more than 2 million repositories, and searching these open source repositories is available for free on [Sourcegraph.com](https://sourcegraph.com/search). ## Opting out We believe that having the universe of open source code available all in one place will prove to be an asset for many developers; however, we also understand that this may raise concerns among open source contributors. While we do not offer the ability to opt out of our open source code index today, it is something that we have heard requested from the community. Our teams are exploring solutions and are eager to partner with the larger community on how we implement an opt-out. This section will be updated as soon as we have more information to provide, but in the meantime, please reach out to us at [feedback@sourcegraph.com](mailto:feedback@sourcegraph.com). # Sourcegraph.com [Sourcegraph.com](https://sourcegraph.com/search) lets you search 2 million open source repositories. To use Sourcegraph on your own (private) code, [use Sourcegraph Cloud](/cloud/) or [deploy self-hosted Sourcegraph](/admin/deploy/). - [Indexing open source code in Sourcegraph.com](/dotcom/indexing_open_source_code) > Note: Sourcegraph.com is a special instance of Sourcegraph with some different behavior compared to that on Sourcegraph Cloud and self-hosted instances. If you're curious about the differences, search our codebase for [`envvar.SourcegraphDotComMode()`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+envvar.SourcegraphDotComMode%28%29&patternType=keyword). > > For example, global searches do not search unindexed code by default on sourcegraph.com, whereas on a cloud or self-hosted instance this isn't the case. > > To learn more about where dotcom handles things differently checkout use of the [SourcegraphDotcomMode](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+%22if+envvar.SourcegraphDotComMode%28%29%22&patternType=keyword) env var in our codebase! # Cody Pricing Plans

Learn about the different plans available for Cody.

Cody provides three subscription plans: **Free**, **Pro**, and **Enterprise**. Each plan is aimed to cater to a diverse range of users, from individual projects to large-scale enterprises. Cody Free includes basic features, while the Pro and Enterprise plans offer additional advanced features and resources to meet varying user requirements. The free plan is designed for individuals to get started with Cody. It comes with a set of features to enhance your coding experience. It includes **unlimited autocompletion suggestions** per month, covering both whole and multi-line suggestions. You also get **200 chats/prompts** per month with access to creating custom prompts. The free plan provides access to local context to enhance Cody's understanding of your code and ability to provide accurate suggestions. Local context uses keyword search to search the local repository (the one currently open in your IDE). Cody Free uses the DeepSeek-Coder-V2 model for autocomplete. It uses the Claude 3.5 Sonnet (New) model for chat and prompts, and users have several other model options they can switch to. See [Supported LLMs](https://sourcegraph.com/docs/cody/capabilities/supported-models) for more information. ### Billing cycle There is no billing cycle for Cody Free, as it is free to use. If you complete your monthly chat/prompts limit, you'll be prompted to upgrade to the Pro plan. Otherwise, you'll have to wait approximately 30 days for the limits to reset. The reset date is based on your sign-up date. For example, if you sign up on December 15th, your limit will reset on January 15th. ### Upgrading from Free to Pro ![upgrade-cody-pro](https://storage.googleapis.com/sourcegraph-assets/Docs/upgrade-cody-pro.png) To upgrade from Cody Free to Pro: - Log in to your [Cody dashboard](https://sourcegraph.com/cody/manage) - Click the **Upgrade** button in the top right corner - It takes you to the [Subscription plans](https://sourcegraph.com/cody/subscription) page - Under the Pro tier, click **Get Pro** - Enter your card details for billing - Click confirm and upgrade to Cody Pro for **$9 per month** Once your Pro subscription is confirmed, click **My subscription** to manage and view your activation details, or click **Dashboard** for the overall view. Cody Pro, designed for individuals or small teams at **$9 per user per month**, offers an enhanced coding experience beyond the free plan. It provides unlimited autocompletion suggestions plus increased limits for chat and prompts. It also uses local repository context to enhance Cody's understanding and accuracy. Cody Pro uses DeepSeek-Coder-V2 by default for autocomplete. Pro accounts also default to the Claude 3.5 Sonnet (New) model for chat and prompts, but users can switch to other LLM model choices. You can refer to the [supported LLM models](/cody/capabilities/supported-models) docs for more information. Support for Cody Pro is available through our Support team via support@sourcegraph.com, ensuring prompt assistance and guidance. ### Downgrading from Pro to Free To revert back to Cody Free from Pro: - Go to your Sourcegraph dashboard **Cody > Dashboard** - Next, **Manage subscription** that takes you to **Cody > Subscription** - Clicks **Cancel** on the Pro tier to cancel your Pro subscription - This automatically downgrades you to Cody Free once after your billing cycle ends ### Upgrading from Pro to Enterprise To upgrade from Cody Pro to Cody Enterprise, you should [Contact Sales](https://sourcegraph.com/contact/request-info) and connect with one of our account teams. They will help you set up your account and start with Cody Enterprise. ## Billing FAQs for Cody Pro ### What is Cody's pricing plan? You can get this info from our [pricing page](https://sourcegraph.com/pricing?product=cody). ### When are payments taken? We charge payments at the beginning of each billing cycle, and they get automatically renewed once you've added your credit card details. ### What is a billing cycle? The billing cycle refers to the start date you start your Cody Pro plan. Your Cody Pro plan will automatically renew at each billing date. You can view your current and previous billing cycles from the **My subscription** tab. ### What payment methods are available? We currently only support payments through a credit card. ### What happens when I cannot pay or don't want to renew the Pro plan? If you do not want to renew Cody Pro, you can cancel your Cody Pro subscription at any time by going to the [accounts page](https://accounts.sourcegraph.com/cody/subscription). If you cancel the Pro plan, your Cody Pro will continue until the end of the billing cycle. Once the billing cycle ends, your plan will not renew, and your card will not be charged. This automatically downgrades your plan from Cody Pro to Cody Free. If you change your mind after canceling your plan please contact our Support team at support@sourcegraph.com and we can help you get re-subscribed. ### Are there any refunds for the Pro subscription? We don't offer refunds, but if you have any queries regarding the Cody Pro plan, please write to support@sourcegraph.com, and we'll help resolve the issue. ### How do I access previous invoices? You can access your invoices via the [Cody Dashboard](https://sourcegraph.com/cody/manage) and clicking "Manage Subscription". Note that invoices are not emailed every month. ## Enterprise Starter Cody Pro users can also switch to the Enterprise Starter plan for **$19 per user per month**. This plan includes all the features of Cody Pro plus a multi-tenant Sourcegraph instance with core features like a fully managed version of Sourcegraph (AI + code search + intent detection with integrated search results, with privately indexed code) through a self-serve flow. Read more about the [Enterprise Starter plan](/pricing/enterprise-starter). Cody Enterprise is designed for enterprises prioritizing security and administrative controls. We offer either seat-based or token based pricing models, depending on what makes most sense for your organization. You get Claude Haiku 3.5 and Claude Sonnet 3.5 as the default LLM models without extra cost. You also get additional capabilities like BYOLLM (Bring Your Own LLM), supporting Single-Tenant and Self Hosted setups for flexible coding environments. Security features include SAML/SSO for enhanced authentication and guardrails to enforce coding standards. Cody Enterprise supports advanced Code Graph context and multi-code host context for a deeper understanding of codebases, especially in complex projects. With 24/5 enhanced support, Cody Enterprise ensures timely assistance. ## Billing FAQs for Cody Enterprise ### How are active users calculated for Cody? ### Billable active users This only applies to Enterprise Cody users. Cody Pro users pay for a seat every month, regardless of usage. A billable active Cody user is signed in to their Enterprise Sourcegraph account and actively interacts with Cody (for example, they see suggested autocompletions, run commands, or chat with Cody, they start new discussions, clear chat history, or copy text from chats. They change Cody's settings and more). Having Cody installed and signing in is not enough to be considered a billable user. Specific categories of user actions that qualify as active Cody usage for billing include, but are not limited to: - Signing in to Cody in an editor - Seeing suggested autocompletions and/or accepting/rejecting them - Running commands or chatting with Cody - Starting new chats, clearing chat history, copying text from chats or otherwise interacting with the Cody chat interface in the editor or on the web - Adding or changing context in a Cody chat interface or changing models - Interacting with Cody tutorials - Changing Cody personal or team settings ### Authenticated users An authenticated Cody user is authenticated (or signed in) to Cody in their editor within a given time period. This includes all users who interact with Cody on the web. Authenticated users are a superset of [billable users](#billable-active-users) defined above. They include users who are signed in but do not actively engage with Cody. ## Plans Comparison The following table shows a high-level comparison of the three plans available on Cody. | **Features** | **Free** | **Pro** | **Enterprise Starter** | **Enterprise** | | --------------------------------- | ---------------------------------------------------------- | ----------------------------------------------------------------- | -------------------------------------------------- | -------------------------------------------------- | | **Autocompletion suggestions** | Unlimited | Unlimited | Unlimited | Unlimited | | **Chat Executions** | 200 per month | Increased limits | Increased limits | Unlimited | | **Keyword Context (local code)** | Supported | Supported | Supported | Supported | | **Developer Limitations** | 1 developer | 1 developer | Scalable, per-seat pricing | Scalable, consumption-based pricing | | **LLM Support** | [View latest](/cody/capabilities/supported-models) | [View latest](/cody/capabilities/supported-models) | [View latest](/cody/capabilities/supported-models) | [View latest](/cody/capabilities/supported-models) | | **Code Editor Support** | VS Code, JetBrains IDEs, Visual Studio (Preview) | VS Code, JetBrains IDEs, Visual Studio (Preview) | VS Code, JetBrains IDEs, Visual Studio (Preview) | VS Code, JetBrains IDEs, Visual Studio (Preview) | | **Single-Tenant and Self Hosted** | N/A | N/A | N/A | Yes | | **SAML/SSO** | N/A | N/A | N/A | Yes | | **Guardrails** | N/A | N/A | N/A | Yes | | **Advanced Code Graph Context** | N/A | N/A | N/A | Included | | **Multi-Code Host Context** | N/A | N/A | GitHub only | Included | | **Discord Support** | Yes | Yes | Yes | Yes | | **24/5 Enhanced Support** | N/A | N/A | Yes | Yes |
# Troubleshooting Cody

Learn about common reasons for errors that you might run into when using Cody and how to troubleshoot them.

If you encounter errors or bugs while using Cody, try applying these troubleshooting methods to understand and configure the issue better. If the problem persists, you can report Cody bugs using the [issue tracker](https://github.com/sourcegraph/cody/issues), by using the [Support Forum](https://community.sourcegraph.com/), or by asking in the [Discord](https://discord.gg/s2qDtYGnAE) server. ## VS Code extension ### Cody is not responding in chat If you're experiencing issues with Cody not responding in chat, follow these steps: - Ensure you have the latest version of the [Cody VS Code extension](https://marketplace.visualstudio.com/items?itemName=sourcegraph.cody-ai). Use the VS Code command `Extensions: Check for Extension Updates` to verify - Check the VS Code error console for relevant error messages. To open it, run the VS Code command `Developer: Toggle Developer Tools` and then look in the `Console` for relevant messages ### Cody responses/completions are slow If you're experiencing issues with Cody's responses or completions being too slow: - Ensure you have the latest version of the [Cody VS Code extension](https://marketplace.visualstudio.com/items?itemName=sourcegraph.cody-ai). Use the VS Code command `Extensions: Check for Extension Updates` to verify - Enable verbose logging, restart the extension and reproduce the issue seen again (see below `Access Cody logs` for how to do this) - Send information to the our Support Team at support@sourcegraph.com Some additional information that will be valuable: - Where are you located? Any proxies or firewalls in use? - Does this happen with multiple providers/models? Which models have you used? ### Access Cody logs VS Code logs can be accessed via the **Outputs** view. You will need to set Cody to verbose mode to ensure important information to debug is on the logs. To do so: - Go to the Cody Extension Settings and enable: `Cody › Debug: Verbose` - Restart or reload your VS Code editor - You can now see the logs in the Outputs view - Open the view via the menu bar: `View > Output` - Select **Cody by Sourcegraph** from the dropdown list - You can export the logs by using the command palette (Cmd+Shift+P on Mac, Ctrl+Shift+P on Windows/Linux) and searching for the "Cody Debug: Export Logs" command ![View Cody's autocomplete logs from the Output View in VS Code](https://storage.googleapis.com/sourcegraph-assets/Docs/autocomplete-logs.png) ### Errors trying to install Cody on macOS If you encounter the following errors: ```bash Command 'Cody: Set Access Token' resulted in an error Command 'cody.set-access-token' not found ``` Follow these steps to resolve the issue: - Close your VS Code editor - Open your Keychain Access app - Search for `cody` - Delete the `vscodesourcegraph.cody-ai` entry in the system keychain on the left - Reopen the VS Code editor. This should resolve the error ![Opening up Keychain Access](https://storage.googleapis.com/sourcegraph-assets/blog/cody-docs-troubleshooting-keychain-access.png) ### Signin fails on each VS Code restart If you find yourself being automatically signed out of Cody every time you restart VS Code, and suspect it's due to keychain authentication issues, you can address this by following the steps provided in the official VS Code documentation on [troubleshooting keychain issues](https://code.visualstudio.com/docs/editor/settings-sync#_troubleshooting-keychain-issues). These guidelines should help you troubleshoot and resolve any keychain-related authentication issues, ensuring a seamless experience with Cody on VS Code. ### No context files were included by Cody If Cody's answers don't seem accurate, it may be because Cody is unable to find the right relevant files to use as context. You can see which files Cody used in the **Context** row right below your message. To troubleshoot further: 1. Enable the `cody.debug.verbose` setting in your VS Code user settings. 1. Open the **Cody by Sourcegraph** output channel in VS Code. 1. Look for log messages such as the following: ```bash █ SimpleChatPanelProvider: getEnhancedContext > search █ symf: using downloaded symf "/Users/beyang/Library/Application Support/Code/User/globalStorage/sourcegraph.cody-ai/symf/symf-v0.0.6-aarch64-macos" █ SimpleChatPanelProvider: getEnhancedContext > search (end) █ DefaultPrompter.makePrompt: Ignored 8 additional context items due to limit reached ``` ### Rate limits Cody Free provides **unlimited autocomplete suggestions** and **200 chat invocations** per user per month. On Cody Pro and Enterprise plans, usage limits are increased, and controlled by **Fair Usage**. This means that some users occasionally experience a limitation placed on their account. This limitation resets within 24 hours. If this issue persists, contact us through our [community forum](https://community.sourcegraph.com), Discord, or email support@sourcegraph.com. #### 429 errors A 429 status code means you are on a free account and hit your usage limit/quota for the day. It can also mean you were sending too many requests in a short period of time. If you have Cody Pro and you are seeing 429 errors, you can contact us at [support@sourcegraph.com](mailto:support@sourcegraph.com) to resolve this. ### Error logging in VS Code on Linux and Windows If you encounter difficulties logging in to Cody on Linux using your Sourcegraph instance URL, along with a valid access token, and notice that the sign-in process in VS Code hangs, it might be related to underlying networking rules concerning SSL certificates. To address this, follow these steps: - Close your VS Code editor - In your terminal, type and run the following command: `echo "export NODE_TLS_REJECT_UNAUTHORIZED=0">> ~/.bashrc` - Restart VS Code and try the sign in process again On Windows, - Close Visual Studio Code - In your Command Prompt or PowerShell window, run the following command: `setx NODE_TLS_REJECT_UNAUTHORIZED 0` - Restart Visual Studio Code and try the sign in process again ### Cloudflare Request Failed If you encounter this error: ``` Request Failed: Request to https://sourcegraph.com/.api/completions/stream?api-version=1&client-name=vscode&client-version=1.34.3 failed with 403 Forbidden ``` It indicates that our web application firewall provider, Cloudflare, has flagged the source IP as suspicious. Consider disabling anonymizers, VPNs, or open proxies. If using a VPN is essential, you can try [1.1.1.1](https://one.one.one.one), which is recognized to be compatible with our services. ### Error with Cody `contextFilters` during chat or editing code The `contextFilters` setting in Cody is used to control which files are included or excluded when Cody searches for relevant context while answering questions or providing code assistance. Sometimes, you can see the following error: ``` Edit failed to run: file is ignored (due to cody.contextFilters Enterprise configuration setting) ``` This error occurs when you're trying to work with a file that's been excluded by Cody's enterprise-level `contextFilters` configuration. At times, this can happen to files that also haven't been excluded. First, check with your organization's admin to understand which files are excluded. If the error occurs with a file that's not been excluded, the workaround is to uninstall the Cody plugin, restart your IDE and then reinstall the latest version of the extension. This should clear the error. ### VS Code Pro License Issues If VS Code prompts you to upgrade to Pro despite already having a Pro license, this usually happens because you're logged into a free Cody/Sourcegraph account rather than your Pro account. To fix this: - Check which account you're currently logged into - If needed, log out and sign in with your PRO account credentials ### Error exceeding `localStorage` quota When using Cody chat, you may come across this error: ```bash Failed to execute 'setItem' on 'Storage': Setting the value of 'user-history:$user_id' exceeded the quota. ``` This error indicates that the chat history size surpasses the capacity of your browser's local storage. Cody stores comprehensive context data with each chat message, contributing to this limitation. To fix this, navigate to https://sourcegraph.your-domain.com/cody/chat and click `Clear Chat History` if your instance is on v5.2.3+. For older versions, clear your browsing data or browser history. ### Record performance traces for Cody You can get performance traces from the Cody VS Code extension in production with full support for source maps. To do so: - Start VS Code with a special runtime flag. In macOS, you can do so via the terminal like this: ```bash /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron --inspect-extensions=9333 ``` Note that you may need to quit VSCode first, then run that command to re-launch it. It will open all of your windows and tabs again. - After VS Code is started, head over to Chrome and go to `chrome://inspect`, which takes you to the following: ![](https://gist.github.com/assets/458591/0a17881b-5449-48d5-a53e-5556f4f2dedd) - Configure the inspect server you started on port `9333` from here. To do so, click on **Open dedicated DevTools for Node**, then go to the **Connection** tab, and make sure to add `localhost:9333` to the list ![](https://gist.github.com/assets/458591/972ce113-88f0-482a-99b7-5e51957981ef) - Now head back to the `chrome://inspect` tab, and you should see a new remote target that you can inspect ![](https://gist.github.com/assets/458591/06b2e293-aea7-42e8-a9cc-592863b6fb07) - Clicking this will open a (somewhat reduced) DevTools view. Great! We've almost got it. From here you can go to the **Performance** tab to record a trace. And finally, swap tabs to the VS Code window and interact with the extension. Come back later to stop the recording and export it. ![](https://gist.github.com/assets/458591/d590636b-31e5-4436-8039-ee62b7a8e59f) ### Record a CPU profile for Cody in VSCode If you are experiencing performance issues with Cody in VSCode, recording a CPU profile can help diagnose the problem. Here’s how you can capture a CPU profile: 1. **Open Developer Tools**: - In VSCode, go to the Command Palette (`Ctrl+Shift+P` on Windows/Linux, `Cmd+Shift+P` on macOS). - Type `Developer: show running extensions` and select it to open running extensions. 2. **Record the CPU Profile**: - There you should see Cody in the list. Right click on it and start a host profile. - This will show you the running profile in the bottom right of the window. - Reproduce the issue you are experiencing with Cody. - Once you have reproduced the issue, click the message in the bottom right you stop the trace. 3. **Save the CPU Profile**: - After stopping the trace, you need to go back to the Developer: show running extensions view. - Right click Cody and click `Save extension profile`. 4. **Share the CPU Profile**: - Attach the `.cpuprofile` file to your issue report on GitHub or share it with the support team for further analysis. Following these steps will help the team understand and resolve the performance issues more effectively. ## JetBrains IntelliJ extension ### Access Cody logs JetBrains logs can be accessed via the **Output** panel. To access logs, you must first enable Cody logs from the Settings menu. To do so: - Open the Settings panel (`⌘,` for macOS) (`Ctrl+Alt+S` for Windows/Linux) - Go to `Sourcegraph & Cody` - Click on `Cody` - Check the box to Enable debug - Optionally, select the box to enable Verbose debug - Click Apply - To access the logs, go to Help - Show Log in Finder - Open the `idea.log` file ### High CPU Usage with Cody Agent If you notice the Cody agent reaching 100% CPU utilization, try the following: 1. Disable the Cody plugin. 1. Re-enable the plugin. This simple action of turning the plugin off and on again can often resolve the high CPU usage issue. ## Android Studio extension ### Cody cannot start. Stuck on spinning icon. This issue occurs because JCEF isn't supported in Android Studio and causes Cody to not start. The suggested workaround is to: 1. Go to `Help` > `Find Action: Registry`. 1. Scroll to `ide.browser.jcef.sandbox.enable`. 1. Disable that key and close. 1. Then go to `Help` > `Find Action: Choose Boot runtime for the IDE`. 1. Select the last option. 1. Restart Android Studio. ## Visual Studio extension ### Access Cody logs Visual Studio logs can be accessed via the **Output** panel. To access logs: - Open `View -> Output` from the main menu - Select `Cody` output pane ![Cody Default Output](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-ouput.png) ### Autocomplete Cody autocomplete for Visual Studio uses the underlying VS API to display completions in the editor. It's turned on by default in VS (`Tools -> Options -> IntelliCode -> Show Inline Completions`). Without this setting enabled, autocomplete will not work, so this is the first thing to check. ![Inline completions](https://storage.googleapis.com/sourcegraph-assets/Docs/Inline-completions.png) Also make sure that `Tools -> Options -> Cody -> Automatically trigger completions` is turned on (it is by default). ![Autocomplete](https://storage.googleapis.com/sourcegraph-assets/Docs/Autocomplete.png) Autocomplete is supported from Visual Studio 17.6+ and includes support for the following languages: 1. C/C++/C# 2. Python 3. JavaScript/TypeScript/TSX 4. HTML 5. CSS 6. JSON #### Non-trusted certificates If autocomplete still doesn't work (or the Cody Chat), you could try **turning on** the option to `accept non-trusted certificates` (requires Visual Studio restart). This should help, especially in enterprise settings if you are behind a firewall. ![Non-trusted-certificates](https://storage.googleapis.com/sourcegraph-assets/Docs/Non-trusted-certificates.png) ### Detailed debugging logs The detailed logging configuration can be turned on by adding the `CODY_VS_DEV_CONFIG` environment variable containing the full path to [the configuration file](https://github.com/sourcegraph/cody-vs/blob/main/src/CodyDevConfig.json) placed somewhere in the filesystem. ![Detailed logs](https://storage.googleapis.com/sourcegraph-assets/Docs/Detailed-logs.png) Two additional output panes, `Cody Agent` and `Cody Notifications`, will be created with more detailed logs. More information on how to configure them is available [here](https://github.com/sourcegraph/cody-vs/blob/main/CONTRIBUTING.md#developer-configuration-file). ![Cody output panes](https://storage.googleapis.com/sourcegraph-assets/Docs/Cody-output-panes.png) ## Regular Expressions ### Asterisks being removed If you send Cody a prompt with a query string like `$filteredResults = preg_grep('*\.' . basename($inputPath) . '\.*', $fileList);`, the asterisks may be removed because Cody interprets the content as a literal string rather than code. When sharing code with Cody, wrap your code in triple backticks (```) to ensure it's recognized as a code block rather than plain text. For example: ````regex ``` $filteredResults = preg_grep('*\.' . basename($inputPath) . '\.*', $fileList); ``` ```` ## Forked Repos As Context If you would like to add a forked repository as Cody context, you may need to add `"search.includeForks": true` to the [global settings](/admin/config/settings#editing-global-settings-for-site-admins) for your instance. {/* ## Eclipse extension ### See a white screen the first time you open Cody chat This can happen if Eclipse prompts you to set up a password for secure storage and Cody timeouts while waiting. Simply close and re-open the Cody chat. ### "No password provided" in the error log If you see this error in the error log, it happens because the default OS password integration has been corrupted. Go to **Preferences > General > Security > Secure Storage** and ensure your OS integration is checked. Then click **Clear Passwords** at the top, and then click **Change Password**. If you see a dialog saying **An error occurred while decrypting stored values... Do you want to cancel password change?** Click **No**. This will reset the secure storage master password for OS integration. You will be asked if you want to provide additional information for password recovery, which is optional. Click **Apply and Close** and then restart Eclipse. ### General Tips You can open the Cody Log view using the same steps as above, but instead, select **Cody Log**. ![cody-logs](https://storage.googleapis.com/sourcegraph-assets/Docs/eclipse-cody-log-1124.png) This will include more information about what Cody is doing, including any errors. There is a copy button at the top right of the log view that you can use to copy the log to your clipboard and send it to us. Be careful not to include any sensitive information, as the log communication is verbose and may contain tokens. Additionally, Eclipse's built-in Error Log can be used to view any uncaught exceptions and their stack traces. You can open the Error Log using the **Window > Show View > Error Log** menu. */} ## OpenAI o1 ### Context Deadline Exceeded Error Symptoms: - "Request Failed: Request to... failed with 500 Internal Server Error: context deadline exceeded" - Happens even with relatively small inputs (~220 lines) Solutions: - Keep input context smaller - aim for less than 200 lines of code initially - Break down larger requests into smaller chunks - Start a new chat session if errors persist - Add "Keep your answer brief!" to prompts when possible Prevention: - Import only the most relevant files - Use file range syntax (e.g., @file:1-100) to limit context - Focus on specific sections rather than entire codebases ### Truncated Outputs Symptoms: - Response cuts off mid-sentence - Unable to get complete code examples - "Continue" requests also result in truncation Solutions: - Break down complex requests into smaller steps - Consider using Sonnet 3.5 for tasks requiring longer outputs Limits: - Input tokens: 45k - Output tokens: 4k ### Model Switching Issues Symptoms: - Model reverts to Sonnet 3.5 unexpectedly - "On waitlist" message appears after previously having access - Unable to select o1 models in command palette Solutions: - Restart IDE/VS Code - Sign out and sign back in - Check Pro subscription status - Contact support if issues persist ### Response Format Errors Symptoms: - "Request Failed: Unexpected response format" - Model stops responding - Inconsistent output formatting Solutions: - Cancel and retry the request - Start a new chat session - Reduce context complexity - Use one-shot prompts with clear requirements
# Cody Quickstart

This quickstart guide shows how to use Cody in the VS Code editor. You'll learn about the following tasks:

- Chat with Cody to ask questions about your code - Code completions and suggestions - Use Cody to refactor code - Use Cody to debug code and ask Cody to fix bugs There are [many ways to use Cody](/cody/clients). For this guide, we'll be using the [VS Code](/cody/clients/install-vscode) extension. ## Prerequisites Before you start, you'll need the following: - [Cody extension installed](/cody/clients/install-vscode) in your VS Code editor - Free or Pro account via Sourcegraph.com or a Sourcegraph Enterprise account - A project open in VS Code ## Getting started with Cody After installing the extension and connecting to a Sourcegraph instance, you can leverage Cody to use the best LLM and context to understand, write, and fix code. Click the **Cody** icon from the VS Code side activity bar to open the Cody chat panel. By default, the chat input will have the context of your entire codebase, and Claude 3.5 Sonnet (New) is selected as the default chat model. Based on your [Cody tier](https://sourcegraph.com/pricing?product=cody), you can change the LLM model and context based on your use case to optimize speed, accuracy, or cost. To help you automate your key tasks in your development workflow, you get **[Prompts](/cody/capabilities/commands)**. If you are a part of an organization on Sourcegraph.com or a self-hosted Sourcegraph instance, you can view these pre-built Prompts created by your teammates. Alternatively, you can create your own Prompts via the **Prompt Library** from your Sourcegraph instance. The Cody chat interface offers a few more options and settings. [You can read more in these docs](/cody/capabilities/chat). ![cody-chat-interface](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-chat-interface-0225.jpg) ## Chat with Cody The best way to see Cody in action is through chat. Cody allows you to chat directly with AI to ask questions about your code. You can start by asking simple questions like: - What does this code do? - Explain this codebase in three to four lines Or ask more complex questions like: - Suggest ways to refactor the codebase? - How can I make this code more performant? Cody will respond with a code snippet, a suggested fix, or an explanation. ![chat-with-cody](https://storage.googleapis.com/sourcegraph-assets/Docs/chat-with-cody-1124.png) ## Code completions and suggestions Cody helps you code faster by providing real-time single and multi-line code completions. It uses the context of the code around your cursor to make accurate suggestions and starts predicting what you're trying to write before you type it. Let's try it by typing a `bubbleSort()` function in a JavaScript file. ![cody-completions](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-completions-1124.png) Cody automatically predicts the function body in gray-dimmed text. Press `Tab` to accept the suggestion or `Esc` to dismiss it. ## Use Cody to refactor code You can refactor your code with inline edits. All you need to do is highlight the code, hit the edit hotkey (`Opt + K`), and describe a change. Cody will generate a diff for the change in seconds. Let's use the same `bubbleSort()` function from the previous section. Now, refactor the function to sort dates in descending order. Highlight the function and press `Opt + K`. ![cody-refactor](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-refactor-code-1124.png) Type your refactoring suggestion in the input field and press `Enter`. Cody will quickly generate a diff for the change. You can review the diff, accept the change, reject it, or retry if you are unsatisfied with the result. ## Use Cody to debug code You can ask Cody to debug your code and fix bugs. Cody chat and inline edits are both quite powerful in debugging. If there is a bug, you can ask Cody to debug and fix the code. VS Code and JetBrains IDEs offer **Ask Cody to fix** option. When a mistake occurs, a red warning appears. Along with this, you get a lightbulb icon. If you click on this lightbulb icon, there is an **Ask Cody to fix** option. Click this, and Cody will try to fix the bug with a **Cody is working** notice. ![code-actions](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-code-actions-vscode-1124.png) That's it for this quickstart guide! Feel free to continue chatting with Cody to learn more about its [capabilities](/cody/capabilities).
# Cody Prompting Guide

To get the best results from Cody, whether you're exploring a codebase, summarizing a pull request, or generating code, clear and effective prompts are key. This guide will help you write effective prompts to maximize your experience with Cody.

## Why do prompts matter? Prompts are the foundation of how AI coding assistants like Cody interact with large language models (LLMs). They're not just chat inputs—they guide Cody to give precise, contextual, and actionable code suggestions. While Cody handles a lot of prompt engineering under the hood, the quality of your prompts still plays a key role in shaping the results. So, what defines a great prompt? ## Anatomy of a prompt A well-crafted prompt has all the key elements to guide Cody in delivering accurate and relevant responses. You don’t need to include every element in every prompt, but understanding them can help you write more effectively. Let's split these docs into three parts: 1. **Preparation**: How you'll prepare your code for Cody 2. **Prompting**: How you will create effective prompts for Cody 3. **Example prompts**: Examples of prompts for different use cases ## 1. Preparation Before you start writing prompts, preparing your codebase for Cody is essential. Here are some key preparation steps: ## Treat Cody like a new team member When using Cody, it's helpful to treat it like a new team member unfamiliar with your codebase. This approach ensures you provide Cody with the necessary context and information to generate accurate and contextually aware answers. You should focus on providing Cody with all the necessary information, such as the codebase structure, function names, and any relevant docs. The more context and details you provide, the better Cody can assist you in generating relevant and accurate code. For example, ❌ Instead of a vague prompt like: ``` How can I filter products in JavaScript? ``` ✅ Provide a more specific prompt with details: ``` I have an array of product objects in JavaScript, each with the following properties: id, name, category, and price. How can I write a function to filter the products by category? ``` ## Define a persona or role Specify a persona or role in your prompt to provide an extra layer of context to guide Cody. For example, asking Cody to act as a **beginner Python developer** can result in simpler, more beginner-friendly code snippets. ## Choose descriptive variable names Using clear and descriptive names for variables, functions, and classes is essential for making your code readable and understandable for you and Cody. Avoid abbreviations or ambiguous names that may confuse your AI assistant. ✅ Good example: ```php function calculateTotalPrice($items, $taxRate) { // ... } ``` ❌ Bad example: ```php function calc($i, $t) { // ... } ``` ## Write clear code comments and docstrings In addition to variable names, comments, and docstrings are crucial in guiding Cody's understanding of your code. Treat them as a way to communicate with Cody, just like you would with a new engineer. Explain complex logic, algorithms, or project-specific concepts to give Cody context. ✅ Good example: ```javascript /** * Calculate the shipping cost based on the order total. * - For orders under $50, the shipping cost is $5. * - For orders between $50 and $100, the shipping cost is $10. * - For orders above $100, shipping is free. * * @param {number} orderTotal - The total amount of the order. * @returns {number} The shipping cost, determined by the following rules: */ function calculateShippingCost(orderTotal) { // Cody will autocomplete here } ``` A bonus here is that Cody can generate these docstrings for you, so you don't have to write them manually. You can use the **document-code** prompt to do this. ## @-mention context Cody leverages the `@-mention` syntax to source context via files, symbols, web URLs, and more. By default, Cody will automatically detect context from the codebase you're working in via pre-filled context chips in the chat input field. Make sure that when you are working with any codebase, Cody picks up the default context. An empty context chip means Cody will search based on generally available context. You can learn more about context [here](/cody/core-concepts/context). ### Indexing your repositories for context @-mention local and current repositories are only available if you have your repository indexed. Enterprise and Enterprise Starter users can request their admins to add their local project for indexing to get access to @-mention context. Repository indexing is only available to supported [Code Hosts](https://sourcegraph.com/docs/admin/code_hosts), please reach out to your admins if you require assistance with indexing. ## Selecting the right LLM Cody offers a variety of LLMs for both chat and in-line edits by all the leading LLM providers. Each LLM has its strengths and weaknesses, so it is important to select the right one for your use case. For example, Claude 3.5 Sonnet and GPT-4o are powerful for code generation and provide accurate results. However, Gemini 2.0 Flash is a decent choice for cost-effective searches. So, you can always optimize your choice of LLM based on your use case. Learn more about all the supported LLMs [here](/cody/capabilities/supported-models). ## 2. Prompting Now that your code is well-prepared, let's focus on writing effective prompts for Cody via the following best practices: ## Provide specific information When using Cody chat, provide as much detail as possible. Include information about the problem, expected behavior, constraints, and project-specific requirements. Be sure to include comprehensive details about the problem, what you expect to happen, any limitations, and specific requirements related to your project. ❌ Bad example: ``` How do I calculate discounts based on loyalty points in Laravel? ``` ✅ Good example: ``` I need to calculate discounts based on customer loyalty points. - If the customer has loyalty points above 500, apply a 10% discount. - If the customer has loyalty points between 200 and 500, apply a 5% discount. - If the customer has loyalty points below 200, no discount is applied. Create a function that takes the total amount and loyalty points as input and returns an object with the discount percentage and discount amount. ``` ## Provide specific context While preparing your codebase for Cody, you learned about the importance of context chips. In addition to this default context, you can provide additional and more specific context to help Cody better understand your codebase. You can continue to `@-mention` files, symbols, and other context sources (as supported by your Cody tier) to make your search more specific and granular. You should approach this as if explaining the situation to a new team member. You should: - Reference important files and symbols - Provide examples from other similar functions ## Provide examples and test cases You should include examples and test cases when applicable to clarify your expectations. Demonstrate edge cases or handling of special conditions to guide Cody in generating robust code. ❌ Bad example: ``` I need to validate email addresses in JavaScript ``` ✅ Good example: ``` Create a function to validate email addresses in JavaScript. Return true for valid email addresses and false for invalid ones. Here are some example test cases: Valid: - "john@example.com" - "jane.doe@example.co.uk" Invalid: - "john@example" - "jane.doe@example." - "invalid.email" Please write the function ``` ## Iterate and refine Start with a general prompt and incrementally add more details based on Cody's responses. Take advantage of the fact that you can chat with Cody. Bring the back-and-forth conversation, especially if you didn't like Cody's first response. Review the generated code or suggestions, provide feedback, and refine your prompts to get the desired results. Initial prompt: ``` I need to calculate the total price of an order. Help me write the function ``` Refined prompt: ``` Thanks. I forgot to mention: - The function should take an array of order items as input - We need to apply a 10% discount if the total price exceeds $100 - Final total should be rounded to two decimal places Please update the function ``` ## Leverage Cody's capabilities You can utilize many other [Cody's capabilities](/cody/capabilities) for generating boilerplate code, common patterns, and repetitive tasks. Prompt it to assist with unit tests, docs, and error handling to save time and ensure code quality. ✅ Good example: ``` Help me write tests for the `calculateAverageRating` function. Here are the test cases I have in mind: - Empty ratings array should return 0 - Array with one rating should return that rating - Array with multiple ratings should calculate the average correctly Make sure the tests cover any edge cases or potential issues. ``` You can also use the **generate-unit-tests** prompt to generate tests for your code. ## Miscellaneous information Try adding any supplementary details regarding comments, debugging guidelines, error management, required dependencies, or coding styles. For instance, when directing Cody to implement a database query in SQL, specify the need for parameterized queries to prevent SQL injection vulnerabilities and provide suggestions for optimizing query performance. ## Prompts Library To accelerate and automate your work, you can leverage Cody's Prompt Library, which helps you build customizable building blocks (prompts), share your best prompts with your teammates, and enable site administrators to promote the best prompts to the rest of the organization. The Prompt Library is a system for creating and sharing customizable prompts. It is explicitly designed for scalability, repeatability, and flexibility. Learn more about the [Prompts and the Prompt Library here](/cody/capabilities/commands). ## Example Prompts Let's examine some examples of good and reusable prompts that you can create via the Prompt Library. This prompt can help if Cody needs to generate code in the correct language/framework version. For example, >Cody generates code snippets using a newer [Spring](https://spring.io/) version that's incompatible with your legacy Java application, which runs Spring 3.x. This mismatch leads to examples utilizing features from Spring Boot 2.x that your project doesn't support. The prompt to tackle this issue looks like this: ``` Please generate Java code that is compatible with the following specifications: Java version: 1.6 Spring framework version: 3.x Testing framework: JUnit 4" ``` This prompt can help if Cody needs to follow your organization's coding standards or style guide. For example, >Your organization follows a specific Python coding style guide that mandates using snake_case for variable names and requires docstrings for all functions. However, Cody might generate code that doesn't adhere to these conventions, leading to inconsistency in your codebase. The prompt to resolve this inconsistency will be: ``` Generate Python code that adheres to our organization's style guide, which includes the following requirements: Use snake_case for all variable and function names. Include docstrings for all functions, describing their purpose and parameters. Follow PEP 8 guidelines for code formatting, including indentation and line length. ``` This prompt can help you if you want to generate unit tests for your code based on a specific format. For example, >You're working with Golang and need Cody to generate unit tests that comply with your team’s conventions. Your organization follows a specific structure for tests, including table-driven tests and `require` from the `testify` package for assertions. However, Cody might provide a simple test that does not align with your standards. While you can use the default `generate-unit-tests` prompt but to get the desired output, you can customize the prompt to include the following: ``` Please generate Golang unit tests that adhere to the following conventions: Use table-driven tests for different input cases. Use the require package from testify for assertions. Follow idiomatic Go test naming conventions and structure. ``` This prompt can help you if you want to onboard new team members by providing them with a set of instructions or guidelines to get started with a project. For example, ``` @repo tell me about this project and how to set it up. ``` To encourage detailed and comprehensive docs of your codebase, you can create a prompt instructing Cody to generate documentation based on your style guide. For example, ``` Please generate a docstring for the given Python function that adheres to our team's documentation standards. The docstring should include: 1. A brief summary of the function's purpose. 2. A description of each parameter, including name, type, and purpose. 3. The return value, including its type and description. 4. Any raised exceptions, if applicable. 5. Example usage of the function, where relevant. ``` You can share all these prompt examples with your team members to help them get started with Cody. If you want others to make the most of these, you can also promote the best prompts.
# Cody Supported on all [Sourcegraph plans](https://about.sourcegraph.com/pricing). Available on VS Code, JetBrains, Visual Studio, and the Web. Cody is an AI coding assistant that uses all the latest LLMs and your development context to help you understand, write, and fix code faster. It uses the powerful Sourcegraph's advanced Search API to pull context from both local and remote codebases so that you can use context about APIs, symbols, and usage patterns from across your entire codebase. Cody connects seamlessly with codehosts like [GitHub](https://github.com/login?client_id=e917b2b7fa9040e1edd4), [GitLab](https://gitlab.com/users/sign_in) and IDEs like [VS Code](/cody/clients/install-vscode), [JetBrains](/cody/clients/install-jetbrains), and [Visual Studio](/cody/clients/install-visual-studio). Once connected, Cody acts as your personal AI coding assistant, equipped with the following capabilities: 1. Developer chat with the most powerful models and context 2. Code completions, code edits, and customizable prompts 3. Extensive context to deliver the most accurate results ## Getting started You can start using Cody with one of the following options: ## Main features Cody's main features include: | **Feature** | **Description** | | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | **[Chat](/cody/capabilities/chat)** | Chat directly with AI to ask questions about your code, generate code, and edit code. Cody has the context of your open file and repository by default, and you can use `@` to add context on specific files, symbols, remote repositories, or other non-code artifacts. | | **[Autocomplete](/cody/capabilities/autocomplete)** | Cody predicts what you're trying to write before you type it. It makes single-line and multi-line suggestions as you type, using the context of the code around your cursor to make accurate suggestions. | | **[Prompts](/cody/capabilities/commands)** | Automate key tasks in your workflow with premade and customizable prompts. Any common query or task can be built into a prompt to save and share with your team. | | **[Context](/cody/core-concepts/context)** | Cody provides the best LLM models and context to power chat. It uses the powerful Sourcegraph's advanced Search API to pull context from both local and remote codebases. | | **[Debug code](/cody/capabilities/debug-code)** | Cody is optimized to identify and fix errors in your code. Its debugging capability and autocomplete suggestions can significantly accelerate your debugging process, increasing developer productivity. | | **[Context Filters](/cody/capabilities/ignore-context)** | Cody can ignore selected repositories from chat and autocomplete results, which helps you control and manage what context your codebase uses. | ## What data is collected, and how is it used? Cody collects and uses data in the following ways: - **Prompts and responses**: When you use Cody, Sourcegraph collects your prompts and responses to provide the service. For individuals using Cody via Sourcegraph.com, Sourcegraph may use your prompts and responses to enhance the user experience, but Sourcegraph does not use any of your data to train models. - **Usage data and feedback**: Sourcegraph also collects usage data and feedback to improve the user experience. Read more about Cody Usage and Privacy policy here → ## Compatible with Sourcegraph products Cody is compatible with other Sourcegraph products, like [Code Search](/code-search/). You can use Cody's chat to ask questions about your codebase. When you run any search query and open a repository or file, you’ll find the **Cody** button that takes you to Cody’s chat interface, which you can use to ask questions about the codebase. [Read more in the Cody FAQs to learn more about such queries →](/cody/faq) ## Join our community If you have any questions regarding Cody, you can always [ask our community](https://community.sourcegraph.com), [Discord](https://discord.com/invite/s2qDtYGnAE), or [create a post on X](https://twitter.com/sourcegraphcody). # Cody FAQs

Find answers to the most common questions about Cody.

## General ### Does Cody train on my code? For Enterprise customers, Sourcegraph will not train on your company’s data. For Free and Pro tier users, Sourcegraph will not train on your data without your permission. Our third-party Language Model (LLM) providers do not train on your specific codebase. Cody operates by following a specific process to generate answers to your queries: - **User query**: A user asks a question - **Code retrieval**: Sourcegraph, our underlying code intelligence platform, performs a search and code intelligence operation to retrieve code snippets relevant to the user's question. During this process, strict permissions are enforced to ensure that only code that the user has read permission for is retrieved - **Prompt to Language Model**: Sourcegraph sends a prompt, and the code snippets are retrieved to a Language Model (LLM). This prompt provides the context for the LLM to generate a meaningful response - **Response to user**: The response generated by the LLM is then sent back to Cody and presented to the user This process ensures that Cody can provide helpful answers to your questions while respecting data privacy and security by not training on or retaining your specific code. ### Does Cody work with self-hosted Sourcegraph? Yes, Cody is compatible with self-hosted Sourcegraph instances. However, there are a few considerations: - Cody operates by sending code snippets (up to 28 KB per request) to a third-party cloud service. By default, this service is Anthropic but can also be OpenAI - To use Cody effectively, your self-hosted Sourcegraph instance must have internet access for these interactions with external services ### Is Cody licensed for private code, and does it allow GPL-licensed code? There are no checks or exclusions for Cody PLG (VS Code, JetBrains) for private and GPL-licensed code. We are subject to whatever the LLMs are trained on. However, Cody can be used with [StarCoder for autocomplete](/cody/clients/enable-cody-enterprise#use-starcoder-for-autocomplete) which is trained only on permissively licensed code. ### Is there a public facing Cody API? Currently, there is no public-facing Cody API available. ### Does Cody require Sourcegraph to function? Yes, Cody relies on Sourcegraph for two essential functions: - It is used to retrieve context relevant to user queries - Sourcegraph acts as a proxy for the LLM provider to facilitate the interaction between Cody and the LLM ### What programming languages does Cody support? Cody supports a wide range of programming languages, including: - JavaScript - TypeScript - PHP - Python - Java - C/C++ - C# - Ruby - Go - SQL - Swift - Objective-C - Perl - Rust - Kotlin - Scala - Groovy - R - MATLAB - Dart - Lua - Julia - COBOL - Shell scripting languages (like Bash, PowerShell) Cody's response quality on a programming language depends on many factors, including the underlying LLM being used. We monitor accuracy metrics across all languages and regularly make improvements. [Let us know](https://community.sourcegraph.com/) if you're seeing poor quality on a particular programming language. ### Can Cody answer non-programming questions? Cody Chat is optimized for coding related use cases and can be used primarily for reviewing, analysis, testing, writing, and editing of software code. Use of Cody for any other purposes is against our [acceptable use policy](https://sourcegraph.com/terms/aup) and may result in your account being restricted. ### What happened to the Cody App? We’ve deprecated the Cody App to streamline the experience for our Cody Free and Cody Pro users.The Cody App is no longer available for download. ## Embeddings ### Why were embeddings removed once my instance was upgraded to v5.3? Cody leverages **Sourcegraph Search** as a primary context provider, which comes with the following benefits: - **More secure**: No code being sent to a third-party embedding API - **Easier to manage**: Less tech debt for embeddings setup and need for refreshes - **More repos**: Sourcegraph Search scales to larger repos and a greater number. Users on Enterprise instances will now be able to select multiple repos as context sources from within the IDE - **Equal, or better, quality**: Sourcegraph Search provides high-quality retrieval, as tested over the last ten years. When a customer sees degradation, we will be ready to respond quickly. We leverage multiple retrieval mechanisms to give Cody the right context and will be constantly iterating to improve Cody's quality. The most important aspect is getting the files from the codebase, not the specific algorithm used to find those files. ### Why are embeddings no longer supported on Cody PLG and Enterprise? Cody does not support embeddings on Cody PLG and Cody Enterprise because we have replaced them with Sourcegraph Search. There are two driving factors: - The need for a retrieval system that can scale across repos and to repos of greater size - A system that is secure and requires low maintenance on the part of users Leveraging Sourcegraph Search allowed us to deliver these enhancements. ## LLM Data Sharing and Retention ### Is any of my data sent to DeepSeek? Our autocomplete features uses the open source DeepSeek-Coder-V2 model, which is hosted by Fireworks.ai in a secure single-tenant environment located in the USA. No customer chat or autocomplete data - such as chat messages, or context such as code snippets or configuration - is stored by Fireworks.ai. Sourcegraph does not use models hosted by DeepSeek (the company), and does not send any data to the same. ## Third party dependencies ### What is the default `sourcegraph` provider for completions? The default provider for completions, specified as `"provider": "sourcegraph"` refers to the [Sourcegraph Cody Gateway](/cody/core-concepts/cody-gateway). The Cody Gateway facilitates access to completions for Sourcegraph enterprise instances by leveraging third-party services such as Anthropic and OpenAI. ### What third-party cloud services does Cody depend on? Cody relies on one primary third-party dependency, i.e., Anthropic's Claude API. Users can use this with the OpenAI API configuration. It's worth noting that these dependencies remain consistent when utilizing the [default `sourcegraph` provider, Cody Gateway](/cody/core-concepts/cody-gateway), which uses the same third-party providers. ### What is the retention policy for Anthropic and OpenAI? Please refer to this [terms and conditions](https://about.sourcegraph.com/terms/cody-notice) for details regarding the retention policy for data managed by Anthropic and OpenAI. ### Can I use my own API keys? Yes, [you can use your own API keys](https://sourcegraph.com/docs/cody/clients/install-vscode#experimental-models). However, this is an experimental feature. Bring-your-own-API-key is fully supported in the Enterprise plan. ### Can I use Cody with my Cloud IDE? Yes, Cody supports the following cloud development environments: - vscode.dev and GitHub Codespaces (install from the VS Code extension marketplace) - Any editor supporting the [Open VSX Registry](https://open-vsx.org/extension/sourcegraph/cody-ai), including [Gitpod](https://www.gitpod.io/blog/boosting-developer-productivity-unleashing-the-power-of-sourcegraph-cody-in-gitpod), Coder, and `code-server` (install from the [Open VSX Registry](https://open-vsx.org/extension/sourcegraph/cody-ai)) ### Can I use my LLM of preference to chat with Cody on CLI? Yes you can. In the CLI you can use the following command to get started. Please replace `$name_of_the_model` with the LLM model of your choice. ``` cody chat --model '$name_of_the_model' -m 'Hi Cody!' ``` For example, to use Claude 3.5 Sonnet, you'd pass the following command in your terminal, `cody chat --model 'claude-3.5-sonnet' -m 'Hi Cody!' ### Sign-in dialog gets stuck with Kaspersky Antivirus **Problem:** When attempting to sign in, users may encounter a perpetual `"Signing in to Sourcegraph..."` dialog that never completes. This issue persists across different VS Code extension versions (e.g. 1.40-1.48) and browsers (Chrome, Edge, Firefox). In the browser console at `accounts.sourcegraph.com/sign-in`, you might see an error: `"Uncaught ApolloError: value.toString is not a function"`. **Solution:** This issue is typically caused by Kaspersky Antivirus interfering with the authentication process. To resolve: - Locate the Kaspersky icon in your system tray - Right-click on the Kaspersky icon - Select "Stop Protection" - Right-click the icon again - Select "Exit" Once you are signed in make sure to re-enable protection. ## OpenAI o1 ### What are OpenAI o1 best practices? #### Context Management - Provide focused, relevant context - Use file references strategically - Keep initial requests concise #### Model Selection - **Sonnet 3.5**: Better for tasks requiring longer outputs #### Prompting Strategy - Be specific and direct - Include complete requirements upfront - Request brief responses when possible - Use step-by-step approach for complex tasks #### Response Time - Start with smaller contexts - Consider breaking complex tasks into stages #### Quality - Provide clear acceptance criteria - Include relevant context but avoid excess - Use specific examples when possible ### What are the known limitations? #### Technical Constraints - 45k input token limit - 4k output token limit - No streaming responses for o1 series - Limited context window
# Cody Tutorial This tutorial gives you a quick walkthrough of leveraging Cody for autocompletion, chat, explaining code, and running commands in VS Code. ## Code Autocomplete ![](https://storage.googleapis.com/sourcegraph-assets/blog/vs-code-onboarding-walkthrough-dec-2023-cody-autocomplete-tsx.gif) Start writing code and Cody will complete the line (or the entire function) for you. Press **Tab** to accept the suggestion. **✨ Pro-tips for using Cody autocomplete** - Autocomplete uses the surrounding code and context to inform the suggestions, so if you need to guide it you can add a comment above the line you're editing. - You can hover over the grey suggestion to see a toolbar of alternative suggestions, as well as other options such as accepting a single word at a time. - You can use the "Trigger Autocomplete at Cursor" command to trigger a code suggestion at any time, using the default keyboard shortcut of `Opt+L`/`Alt+L`. ## Chat ![](https://storage.googleapis.com/sourcegraph-assets/blog/blog-vscode-v018-release/blog-v018-context-controls-002.gif) Answer questions about general programming topics, or specific to your codebase, with Cody chat. You can choose between LLMs, @-mentions files and symbols, and enable enhanced repository-wide code context. You can start a chat at any time using the default keyboard shortcut of `Opt+L`/`Alt+L`. **✨ Pro-tips for using Cody chat** - Tagging specific files and symbols gives the selected LLM more context to help answer your questions more accurately. - If you want to chat directly with the LLM without any additional context adding, delete the @-mentions that are automatically inserted in each new Cody chat and just ask a plain question. ## Cody Commands ![](https://storage.googleapis.com/sourcegraph-assets/blog/vs-code-onboarding-walkthrough-dec-2023-explain.gif) Cody has a range of commands for explaining code, generating unit tests, adding documentation, and more. To get started: select code in your editor, right click, and choose a command from the "Cody" menu. You can also use the [Cody: Commands Menu](command:cody.menu.commands) which has the default keyboard shortcut of `Opt+C`/`Alt+C`. **✨ Pro-tips for using Cody commands** - You can build your own [Custom Commands (Beta)](https://sourcegraph.com/docs/cody/custom-commands) with custom prompts, output into chat or perform code edits, and more. ## Edit Code You can ask Cody to perform code edits using the Edit Code command. This will edit code according to your instructions, giving you the chance to retry the edit with a new prompt, undo, or accept the change. To get started: select code in your editor, right click, and choose "Cody → Edit Code". You can also use the default keyboard shortcut of `Opt+K`/`Alt+K`. **✨ Pro-tips for code editing with Cody** - You can open the 💡 menu, with an "Cody: Edit Code" option, by selecting any line of code and using the keyboard short `Command` `.` on macOS or `Crtl` `.` on Windows & Linux. - If you start an empty line and open the 💡 menu (using the keyboard short `Command` `.` on macOS or `Ctrl` `.` on Windows & Linux) you can use "Cody: Generate Code" to generate new lines of code based on instructions. - You define your own custom code editing commands using [Custom Commands (Beta)](https://sourcegraph.com/docs/cody/custom-commands), by setting the [`commands..mode` property](https://sourcegraph.com/docs/cody/capabilities/commands#commands-id-mode). ## Explain Code Use Cody to get an in-depth explanation of any piece of code in any programming language. To get started, select code in your editor, right-click, and choose "Cody > Explain Code". You can also run this command from the [Cody: Commands Menu](command:cody.menu.commands), which has the default keyboard shortcut of `Opt+C`/`Alt+C`. **✨ Pro-tips for understanding code with Cody** - Cody can also explain errors too, with the "Ask Cody to Explain" option in the 💡 menus, or by right clicking on any items in the "Problems" tab of VS Code. - You can define your own custom code to explain commands to suit, such as a prompt that requests an explanation focused on potential security issues, using [Custom Commands (Beta)](/cody/capabilities/custom-commands). ## Fix Code ![](https://storage.googleapis.com/sourcegraph-assets/blog/vs-code-onboarding-walkthrough-dec-2023-ask-to-fix.gif) Something wrong with your code? Use Cody’s "Ask Cody to Fix" command to fix it, or explain the problem. **✨ Pro-tips for fixing code with Cody** - You can open the 💡 menu, with an "Ask Cody to Fix" option, by moving the cursor over any line of code with an error and using the keyboard short `Command` `.` on macOS or `Crtl` `.` on Windows & Linux. - You can also right click on any items in the "Problems" tab of VS Code and select "Ask Cody to Fix" ## Keyboard Shortcuts Cody offers an extensive set of default key bindings (keyboard shortcuts) to make it easy for you to perform various actions using the keyboard. You can also update these bindings in the `Keyboard Shortcuts Editor`. ### Keyboard Shortcuts Editor The `Keyboard Shortcuts Editor` allows you to easily update the default bindings (keyboard shortcuts). This is particularly useful if the default key bindings conflict with your current or preferred key bindings. By accessing the `Keyboard Shortcuts Editor` in VSCode or `Keymap Shortcuts Editor` in JetBrains, you can search for specific commands, reassign keybindings, and customize your keyboard shortcuts to suit your workflow. ### Getting Started (VSCode) To get started: 1. Click on the **Keyboard Shortcuts** icon in the Cody sidebar under `**SETTINGS AND SUPPORT**` to open the `Keyboard Shortcuts Editor`. 2. In the `Keyboard Shortcuts Editor`, you can search for a specific command by typing in the search bar. 3. Once you have found the command you want to reassign keybindings for, click on it to select it. 4. Follow the instruction on screen to assign a new keybinding. ### Getting Started (JetBrains) To get started: 1. Click on the **IntelliJ IDEA** icon at the top menu bar and then click on **Keymap** from the dropdown to open the `Keymap Shortcuts Editor`. 2. In the `Keymap Shortcuts Editor`, you can search for a specific command by typing in the search bar. 3. Once you have found the command you want to reassign keymappings for, click on it to select it. 4. Follow the instruction on screen to assign a new keymappings. **✨ Pro-tips for assigning keyboard shortcuts for Cody** - You can assign key bindings (keyboard shortcuts) to individual custom commands. # Generate Unit Tests

In this guide, you'll learn how to use Cody to generate unit tests for your codebase.

Writing unit tests for your code is important for a robust and reliable codebase. These unit tests will help you ensure that your code works as expected and doesn't break in the future. Writing these tests can appear cumbersome, but Cody can automatically generate unit tests for your codebase. This guide will teach you to generate a unit test using Cody with real-time code examples. ## Prerequisites Before you begin, make sure you have the following: - Access to Sourcegraph and are logged into your account - Cody VS Code extension installed and connected to your Sourcegraph instance > NOTE: Cody also provides extensions for JetBrains which are not covered in this guide. ## Generate unit tests with Cody One of the prominent commands in Cody is **Generate unit tests** (`/test`). All you need to do is select a code block and ask Cody to write the tests for you. For this guide, let's use an example that converts any `given date` into a human-readable description of the time elapsed between the `given date` and the `current date`. ## Using the `/test` command Cody offers a default `/test` command that you can use to generate unit tests for your codebase. Inside the Cody chat window, type `/test` and select the code block for which you want to generate a unit test. Hit **Enter**, and Cody will start writing a unit test for you. > NOTE: The unit test command is only available in VS Code and JetBrains editor Cody extensions. Let's create a unit test for the `getHumanReadableTime()` function in the `date.js` file. ```js /** * Returns a human-readable string describing the time difference between the given date string and now. * * Calculates the difference in years, months, and days between the given date string and the current date. * Formats the time difference into a descriptive string like "2 years 3 months ago". * Handles singular/plural years/months/days. * If the given date is in the future, returns the string "today". */ export function getHumanReadableTime(dateString) { const startDate = new Date(dateString); const currentDate = new Date(); const years = currentDate.getFullYear() - startDate.getFullYear(); const months = currentDate.getMonth() - startDate.getMonth(); const days = currentDate.getDate() - startDate.getDate(); let timeAgoDescription = ''; if (years > 0) { timeAgoDescription += `${years} ${years === 1 ? 'year' : 'years'}`; } if (months > 0) { if (timeAgoDescription !== '') { timeAgoDescription += ' '; } timeAgoDescription += `${months} ${months === 1 ? 'month' : 'months'}`; } if (days > 0) { if (timeAgoDescription !== '') { timeAgoDescription += ' '; } timeAgoDescription += `${days} ${days === 1 ? 'day' : 'days'}`; } if (timeAgoDescription === '') { timeAgoDescription = 'today'; } else { timeAgoDescription += ' ago'; } return timeAgoDescription; } ``` For such an example, a unit test that checks the basic validation of expected output for recent dates, older dates, current date, and invalid inputs is required. As shown in the demo above, Cody intelligently generated a unit test for you with just a single command. Also, note that with the available context, Cody analyzed the codebase and automatically learned that you are using Vite. So, it tailored the test to be compatible with Vite's testing framework. This shows how Cody leverages context to provide relevant, framework-specific unit tests. If the generated test does not meet your requirements, you can continue the chat thread with follow-up questions, and Cody will help you create a unit test of your choice. ## Trying out the generated test Let's run the generated test to see whether it works or not. Inside your terminal, run the following command: ```bash npm run test ``` All the tests should pass, and you should see the following output: That's it! You've successfully generated a unit test for your codebase using Cody.
# Building Front-End UI with Cody

In this guide, you'll learn how to use Cody to build a user interface for your front-end apps.

## WIP That's it! You've successfully built a front-end using Cody.
# Model Configuration

Learn how to configure Cody via `modelConfiguration` on a Sourcegraph Enterprise instance.

`modelConfiguration` is the recommended way to configure chat and autocomplete models in Sourcegraph `v5.6.0` and later. The `modelConfiguration` field in the **Site config** section allows you to configure Cody to use different LLM models for chat and autocomplete, enabling greater flexibility in selecting the best model for your needs. Using this configuration, you get an LLM selector in the Cody chat with an Enterprise instance that allows you to select the model you want to use. The LLM models available for a Sourcegraph Enterprise instance offer a combination of Sourcegraph-provided models and any custom models providers that you explicitly add to your Sourcegraph instance site configuration. ## `modelConfiguration` The model configuration for Cody is managed through the `"modelConfiguration"` field in the **Site config** section. It includes the following fields: | **Field** | **Description** | | --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | [`sourcegraph`](/cody/enterprise/model-configuration#sourcegraph-provided-models) | Configures access to Sourcegraph-provided models available through Cody Gateway. | | [`providerOverrides`](/cody/model-configuration#provider-overrides) | Configures access to models through your keys with most common LLM providers (BYOK) or hosted behind any openaicompatible endpoint | | [`modelOverrides`](/cody/enterprise/model-configuration#model-overrides) | Extends or modifies the list of models Cody recognizes and their configurations. | | [`selfHostedModels`](/cody/enterprise/model-configuration#self-hosted-models) | Adds models to Cody’s recognized models list with default configurations provided by Sourcegraph. Only available for certain models; general models can be configured in `modelOverrides`. | | [`defaultModels`](/cody/enterprise/model-configuration#default-models) | Specifies the models assigned to each Cody feature (chat, fast chat, autocomplete). | ## Getting started with `modelConfiguration` The recommended and easiest way to set up model configuration is using Sourcegraph-provided models through the Cody Gateway. For a minimal configuration example, see [Configure Sourcegraph-supplied models](/cody/enterprise/model-configuration#configure-sourcegraph-provided-models). ## Sourcegraph-provided models Sourcegraph-provided models, accessible through the [Cody Gateway](/cody/core-concepts/cody-gateway), are managed via your site configuration. For most administrators, relying on these models alone ensures access to high-quality models without needing to manage specific configurations. The use of these models is controlled through the `"modelConfiguration.sourcegraph"` field in the site config. ### Configure Sourcegraph-provided models The minimal configuration for Sourcegraph-provided models is: ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": {} } ``` The above configuration sets up the following: - Sourcegraph-provided models are enabled (`sourcegraph` is not set to `null`) - Requests to LLM providers are routed through the Cody Gateway (no `providerOverrides` field specified) - Sourcegraph-defined default models are used for Cody features (no `defaultModels` field specified) There are three main settings for configuring Sourcegraph-provided LLM models: | **Field** | **Description** | | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | | `endpoint` | (Optional) The URL for connecting to Cody Gateway. The default is set to the production instance. | | `accessToken` | (Optional) The access token for connecting to Cody Gateway defaults to the current license key. | | [`modelFilters`](/cody/enterprise/model-configuration#model-filters) | (Optional) Filters specifying which models to include from Cody Gateway. | ### Disable Sourcegraph-provided models To disable all Sourcegraph-provided models and use only the models explicitly defined in your site configuration, set the `"sourcegraph"` field to `null` as shown in the example below. ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": null, // ignore Sourcegraph-provided models "providerOverrides": { // define access to the LLM providers }, "modelOverrides": { // define models available via providers defined in the providerOverrides }, "defaultModels": { // set default models per Cody feature from the list of models defined in modelOverrides } } ``` ## Default models The `"modelConfiguration"` setting includes a `"defaultModels"` field, which allows you to specify the LLM model used for each Cody feature (`"chat"`, `"fastChat"`, and `"codeCompletion"`). The values for each feature should be `modelRef`s of either Sourcegraph-provided models or models configured in the `modelOverrides` section. If no default is specified or the specified model is not found, the configuration will silently fall back to a suitable alternative. ### Model Filters The `"modelFilters"` section allows you to control which Cody Gateway models are available to users of your Sourcegraph Enterprise instance. The following table describes each field: | **Field** | **Description** | | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `statusFilter` | Filters models based on their release status, such as stable, beta, deprecated or experimental. By default, all models available on Cody Gateway are accessible. | | `allow` | An array of `modelRef`s specifying which models to include. Supports wildcards. | | `deny` | An array of `modelRef`s specifying which models to exclude. Supports wildcards. | The following examples demonstrate how to use each of these settings together: ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": { "modelFilters": { // Only allow "beta" and "stable" models. // Not "experimental" or "deprecated". "statusFilter": ["beta", "stable"], // Allow any models provided by Anthropic, OpenAI, Google and Fireworks. "allow": [ "anthropic::*", // Anthropic models "openai::*", // OpenAI models "google::*", // Google Gemini models "fireworks::*", // Open-source models hosted by Sourcegraph on Fireworks.ai (typically used for autocomplete and Mistral Chat models) ], // Example: Do not include any models with the Model ID containing "turbo", // or any models from a hypothetical provider "AcmeCo" "deny": [ "*turbo*", "acmeco::*" ] } } } ``` ## Provider Overrides A `provider` is an organizational concept for grouping LLM models. Typically, a provider refers to the company that produced the model or the specific API/service used to access it, serving as a namespace. By defining a provider override in your Sourcegraph site configuration, you can introduce a new namespace to organize models or customize the existing provider namespace supplied by Sourcegraph (e.g., for all `"anthropic"` models). Provider overrides are configured via the `"modelConfiguration.providerOverrides"` field in the site configuration. This field is an array of items, each containing the following fields: | **Field** | **Description** | | ------------------ | --------------------------------------------------------------------------------- | | `id` | The namespace for models accessed via the provider. | | `displayName` | A human-readable name for the provider. | | `serverSideConfig` | Defines how to access the provider. See the section below for additional details. | Example configuration: ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": {}, "providerOverrides": [ { "id": "openai", "displayName": "OpenAI (via BYOK)", "serverSideConfig": { "type": "openai", "accessToken": "token", "endpoint": "https://api.openai.com" } }, ], "defaultModels": { "chat": "google::v1::gemini-1.5-pro", "fastChat": "anthropic::2023-06-01::claude-3-haiku", "codeCompletion": "fireworks::v1::deepseek-coder-v2-lite-base" } } ``` In the example above: - Sourcegraph-provided models are enabled (`sourcegraph` is not set to `null`) - The `"openai"` provider configuration is overridden to be accessed using your own key, with the provider API accessed directly via the specified `endpoint` URL. In contrast, models from the `"anthropic"` provider are accessed through the Cody Gateway Refer to the [examples page](/cody/enterprise/model-config-examples) for additional examples. ### Server-side config The most important part of a provider's configuration is the `"serverSideConfig"` field, which defines how the LLM models should be invoked, i.e., which external service or API will handle the LLM requests. Sourcegraph natively supports several types of LLM API providers. The current set of supported providers includes: | **Provider type** | **Description** | | -------------------- | ---------------------------------------------------------------------------------------------------------- | | `"sourcegraph"` | [Cody Gateway](/cody/core-concepts/cody-gateway), offering access to various models from multiple services | | `"openaicompatible"` | Any OpenAI-compatible API implementation | | `"awsBedrock"` | [Amazon Bedrock](https://docs.aws.amazon.com/bedrock/) | | `"azureOpenAI"` | [Microsoft Azure OpenAI](https://azure.microsoft.com/en-us/products/ai-services/openai-service/) | | `"anthropic"` | [Anthropic](https://www.anthropic.com) | | `"fireworks"` | [Fireworks AI](https://fireworks.ai) | | `"google"` | [Google Gemini](http://cloud.google.com/gemini) and [Vertex](https://cloud.google.com/vertex-ai/) | | `"openai"` | [OpenAI](http://platform.openai.com) | | `"huggingface-tgi"` | [Hugging Face Text Generation Interface](https://huggingface.co/docs/text-generation-inference/en/index) | The configuration for `serverSideConfig` varies by provider type. Refer to the [examples page](/cody/enterprise/model-config-examples) for examples. ## Model Overrides With a provider defined (either a Sourcegraph-provided provider or a custom provider configured via the `providerOverrides` field), custom models can be specified for that provider by adding them to the `"modelConfiguration.modelOverrides"` section. This field is an array of items, each with the following fields: - `modelRef`: Uniquely identifies the model within the provider namespace - A string in the format `${providerId}::${apiVersionId}::${modelId}` - To associate a model with your provider, `${providerId}` must match the provider’s ID - `${modelId}` can be any URL-safe string - `${apiVersionId}` specifies the API version, which helps detect compatibility issues between models and Sourcegraph instances. For example, `"2023-06-01"` can indicate that the model uses that version of the Anthropic API. If unsure, you may set this to `"unknown"` when defining custom models - `displayName`: An optional, user-friendly name for the model. If not set, clients should display the `ModelID` part of the `modelRef` instead (not the `modelName`) - `modelName`: A unique identifier the API provider uses to specify which model is being invoked. This is the identifier that the LLM provider recognizes to determine the model you are calling - `capabilities`: A list of capabilities that the model supports. Supported values: `autocomplete`, `chat`, `vision`, `reasoning`, `edit`, `tools`. - `category`: Specifies the model's category with the following options: - `"balanced"`: Typically the best default choice for most users. This category is suited for models like Sonnet 3.5 (as of October 2024) - `"speed"`: Ideal for low-parameter models that may not suit general-purpose chat but are beneficial for specialized tasks, such as query rewriting - `"accuracy"`: Reserved for models, like OpenAI o1, that use advanced reasoning techniques to improve response accuracy, though with slower latency - `"other"`: Used for older models without distinct advantages in reasoning or speed. Select this category if you are uncertain about which category to choose - `"deprecated"`: For models that are no longer supported by the provider and are filtered out on the client side (not available for use) - `contextWindow`: An object that defines the **number of tokens** (units of text) that can be sent to the LLM. This setting influences response time and request cost and may vary according to the limits set by each LLM model or provider. It includes two fields: - `maxInputTokens`: Specifies the maximum number of tokens for the contextual data in the prompt (e.g., question, relevant snippets) - `maxOutputTokens`: Specifies the maximum number of tokens allowed in the response - `reasoningEffort`: Specifies the effort on reasoning for reasoning models (having `reasoning` capability). Supported values: `high`, `medium`, `low`. How this value is treated depends on the specific provider. For example, for Anthropic models supporting thinking, `low` effort means that the minimum [`thinking.budget_tokens`](https://docs.anthropic.com/en/api/messages#body-thinking) value (1024) will be used. For other `reasoningEffort` values, the `contextWindow.maxOutputTokens / 2` value will be used. For OpenAI reasoning models, the `reasoningEffort` field value corresponds to the [`reasoning_effort`](https://platform.openai.com/docs/api-reference/chat/create#chat-create-reasoning_effort) request body value. - `serverSideConfig`: Additional configuration for the model. It can be one of the following: - `awsBedrockProvisionedThroughput`: Specifies provisioned throughput settings for AWS Bedrock models with the following fields: - `type`: Must be `"awsBedrockProvisionedThroughput"` - `arn`: The ARN (Amazon Resource Name) for provisioned throughput to use when sending requests to AWS Bedrock. The ARN format for provisioned models is: `arn:${Partition}:bedrock:${Region}:${Account}:provisioned-model/${ResourceId}`. - `openaicompatible`: Configuration specific to models provided by an OpenAI-compatible provider, with the following fields: - `type`: Must be `"openaicompatible"` - `apiModel`: The literal string value of the `model` field to be sent to the `/chat/completions` API. If set, Sourcegraph treats this as an opaque string and sends it directly to the API without inferring any additional information. By default, the configured model name is sent #### Example configuration ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": {}, "providerOverrides": [ { "id": "huggingface-codellama", "displayName": "huggingface", "serverSideConfig": { "type": "openaicompatible", // optional: disable the use of /completions for autocomplete requests, instead using // only /chat/completions. (available in Sourcegraph 6.4+ and 6.3.2692) // // "useLegacyCompletions": false, "endpoints": [ { "url": "https://api-inference.huggingface.co/models/meta-llama/CodeLlama-7b-hf/v1/", "accessToken": "token" // optional: send custom headers (in which case accessToken above is not used) // (available in Sourcegraph 6.4+ and 6.3.2692) // // "headers": { "X-api-key": "foo", "My-Custom-Http-Header": "bar" }, } ] } } ], "modelOverrides": [ { "modelRef": "huggingface-codellama::v1::CodeLlama-7b-hf", "modelName": "CodeLlama-7b-hf", "displayName": "(HuggingFace) CodeLlama-7b-hf", "contextWindow": { "maxInputTokens": 8192, "maxOutputTokens": 4096 }, "serverSideConfig": { "type": "openaicompatible", "apiModel": "meta-llama/CodeLlama-7b-hf" }, "clientSideConfig": { "openaicompatible": {} }, "capabilities": ["autocomplete", "chat"], "category": "balanced", "status": "stable" } ], "defaultModels": { "chat": "google::v1::gemini-1.5-pro", "fastChat": "anthropic::2023-06-01::claude-3-haiku", "codeCompletion": "huggingface-codellama::v1::CodeLlama-7b-hf" } } ``` In the example above: - Sourcegraph-provided models are enabled (`sourcegraph` is not set to `null`) - An additional provider, `"huggingface-codellama"`, is configured to access Hugging Face’s OpenAI-compatible API directly - A custom model, `"CodeLlama-7b-hf"`, is added using the `"huggingface-codellama"` provider - Default models are set up as follows: - Sourcegraph-provided models are used for `"chat"` and `"fastChat"` (accessed via Cody Gateway) - The newly configured model, `"huggingface-codellama::v1::CodeLlama-7b-hf"`, is used for `"codeCompletion"` (connecting directly to Hugging Face’s OpenAI-compatible API) #### Example configuration with Claude 3.7 Sonnet ```json { "modelRef": "anthropic::2024-10-22::claude-3-7-sonnet-latest", "displayName": "Claude 3.7 Sonnet", "modelName": "claude-3-7-sonnet-latest", "capabilities": [ "chat", "reasoning" ], "category": "accuracy", "status": "stable", "tier": "pro", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 }, "modelCost": { "unit": "mtok", "inputTokenPennies": 300, "outputTokenPennies": 1500 }, "reasoningEffort": "high" }, ``` In this modelOverrides config example: - The model is configured to use Claude 3.7 Sonnet with Cody Gateway - The model is configured to use the `"chat"` and `"reasoning"` capabilities - The `reasoningEffort` can be set to 3 different options in the Model Config. These options are `high`, `medium` and `low` - The default `reasoningEffort` is set to `low` - For Anthropic models supporting thinking, when the reasoning effort is `low`, 1024 tokens is used as the thinking budget. With `medium` and `high` the thinking budget is set to half of the maxOutputTokens value Refer to the [examples page](/cody/enterprise/model-config-examples) for additional examples. ## View configuration To view the current model configuration, run the following command: ```bash export INSTANCE_URL="https://sourcegraph.test:3443" # Replace with your Sourcegraph instance URL export ACCESS_TOKEN="your access token" curl --location "${INSTANCE_URL}/.api/modelconfig/supported-models.json" \ --header "Authorization: token $ACCESS_TOKEN" ``` The response includes: - Configured providers and models—both Sourcegraph-provided (if enabled, with any applied filters) and any overrides - Default models for Cody features #### Example response ```json "schemaVersion": "1.0", "revision": "0.0.0+dev", "providers": [ { "id": "anthropic", "displayName": "Anthropic" }, { "id": "fireworks", "displayName": "Fireworks" }, { "id": "google", "displayName": "Google" }, { "id": "openai", "displayName": "OpenAI" }, { "id": "mistral", "displayName": "Mistral" } ], "models": [ { "modelRef": "anthropic::2024-10-22::claude-3-5-sonnet-latest", "displayName": "Claude 3.5 Sonnet (Latest)", "modelName": "claude-3-5-sonnet-latest", "capabilities": ["chat"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } }, { "modelRef": "anthropic::2023-06-01::claude-3-haiku", "displayName": "Claude 3 Haiku", "modelName": "claude-3-haiku-20240307", "capabilities": ["chat"], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 7000, "maxOutputTokens": 4000 } }, { "modelRef": "fireworks::v1::starcoder", "displayName": "StarCoder", "modelName": "starcoder", "capabilities": ["autocomplete"], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 2048, "maxOutputTokens": 256 } }, { "modelRef": "fireworks::v1::deepseek-coder-v2-lite-base", "displayName": "DeepSeek V2 Lite Base", "modelName": "accounts/sourcegraph/models/deepseek-coder-v2-lite-base", "capabilities": ["autocomplete"], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 2048, "maxOutputTokens": 256 } }, { "modelRef": "google::v1::gemini-1.5-pro", "displayName": "Gemini 1.5 Pro", "modelName": "gemini-1.5-pro", "capabilities": ["chat"], "category": "balanced", "status": "stable", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } }, { "modelRef": "openai::2024-02-01::gpt-4o", "displayName": "GPT-4o", "modelName": "gpt-4o", "capabilities": ["chat"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } }, { "modelRef": "openai::2024-02-01::cody-chat-preview-001", "displayName": "OpenAI o1-preview", "modelName": "cody-chat-preview-001", "capabilities": ["chat"], "category": "accuracy", "status": "waitlist", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } }, ], "defaultModels": { "chat": "anthropic::2024-10-22::claude-3-5-sonnet-latest", "fastChat": "anthropic::2023-06-01::claude-3-haiku", "codeCompletion": "fireworks::v1::deepseek-coder-v2-lite-base" } ``` ## Reasoning models Claude 3.7 and 4 support is available with Sourcegraph versions 6.4+ and 6.3.4167 when using Cody Gateway. This section is primarily relevant to Sourcegraph Enterprise customers using AWS Bedrock or Google Vertex. Reasoning models can be added via `modelOverrides` in the site configuration by adding the `reasoning` capability to the `capabilities` list, and setting the `reasoningEffort` field on the model. Both must be set for the models' reasoning functionality to be used (otherwise the base model without reasoning / exteded thinking will be used.) For example, this `modelOverride` would create a `Claude Sonnet 4 with Thinking` option in the Cody model selector menu, and when the user chats with Cody with that model selected, it would use Claude Sonnet 4's Extended Thinking support with a `low` reasoning effort for the users' chat: ```json { "modelRef": "bedrock::2024-10-22::claude-sonnet-4-thinking-latest", "displayName": "Claude Sonnet 4 with Thinking", "modelName": "claude-sonnet-4-20250514", "contextWindow": { "maxInputTokens": 93000, "maxOutputTokens": 64000, "maxUserInputTokens": 18000 }, "capabilities": [ "chat", "reasoning" ], "reasoningEffort": "low", "category": "accuracy", "status": "stable" } ``` The `reasoningEffort` field is only used by reasoning models (those having `reasoning` in their `capabilities` section). Supported values are `high`, `medium`, `low`. How this value is treated depends on the specific provider: * `anthropic` provider treats e.g. `low` effort to mean that the minimum [`thinking.budget_tokens`](https://docs.anthropic.com/en/api/messages#body-thinking) value (1024) will be used. For other `reasoningEffort` values, the `contextWindow.maxOutputTokens / 2` value will be used. * `openai` provider maps the `reasoningEffort` field value to the [OpenAI `reasoning_effort`](https://platform.openai.com/docs/api-reference/chat/create#chat-create-reasoning_effort) request body value.
# Model Configuration Examples

This section includes examples about how to configure Cody to use Sourcegraph-provided models with `modelConfiguration`. These examples will use the following:

- [Minimal configuration](/cody/enterprise/model-configuration#configure-sourcegraph-provided-models) - [Using model filters](/cody/enterprise/model-configuration#model-filters) - [Change default models](/cody/enterprise/model-configuration#default-models) ## Sourcegraph-provided models and BYOK (Bring Your Own Key) By default, Sourcegraph is fully aware of several models from the following providers: - "anthropic" - "google" - "fireworks" - "mistral" - "openai" ### Override configuration of a model provider Instead of Sourcegraph using its own servers to make LLM requests, it is possible to bring your own API keys for a given model provider. For example, if you wish for all Anthropic API requests to go directly to your own Anthropic account and use your own API keys instead of going via Sourcegraph's servers, you could override the `anthropic` provider's configuration: ```json { "cody.enabled": true, "modelConfiguration": { "sourcegraph": {}, "providerOverrides": [ { "id": "anthropic", "displayName": "Anthropic BYOK", "serverSideConfig": { "type": "anthropic", "accessToken": "token", "endpoint": "https://api.anthropic.com/v1/messages" } } ], "defaultModels": { "chat": "anthropic::2024-10-22::claude-3.5-sonnet", "fastChat": "anthropic::2023-06-01::claude-3-haiku", "codeCompletion": "fireworks::v1::deepseek-coder-v2-lite-base" } } ``` In the configuration above: - Enable Sourcegraph-provided models and do not set any overrides (note that `"modelConfiguration.modelOverrides"` is not specified) - Route requests for Anthropic models directly to the Anthropic API (via the provider override specified for "anthropic") - Route requests for other models (such as the Fireworks model for "autocomplete") through Cody Gateway ### Partially override provider config in the namespace If you want to override the provider config for some models in the namespace and use the Sourcegraph-configured provider config for the rest, you can route requests directly to the LLM provider (bypassing the Cody Gateway) for some models while using the Sourcegraph-configured provider config for the rest. Example configuration ```json { "cody.enabled": true, "modelConfiguration": { "sourcegraph": {}, "providerOverrides": [ { "id": "anthropic-byok", "displayName": "Anthropic BYOK", "serverSideConfig": { "type": "anthropic", "accessToken": "token", "endpoint": "https://api.anthropic.com/v1/messages" } } ], "modelOverrides": [ { "modelRef": "anthropic-byok::2023-06-01::claude-3.5-sonnet", "displayName": "Claude 3.5 Sonnet", "modelName": "claude-3-5-sonnet-latest", "capabilities": ["edit", "chat"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } }, ], "defaultModels": { "chat": "anthropic-byok::2023-06-01::claude-3.5-sonnet", "fastChat": "anthropic::2023-06-01::claude-3-haiku", "codeCompletion": "fireworks::v1::deepseek-coder-v2-lite-base" } } ``` In the configuration above, we: - Enable Sourcegraph-supplied models (the `sourcegraph` field is not empty or `null`) - Define a new provider with the ID `"anthropic-byok"` and configure it to use the Anthropic API - Since this provider is unknown to Sourcegraph, no Sourcegraph-supplied models are available. Therefore, we add a custom model in the `"modelOverrides"` section - Use the custom model configured in the previous step (`"anthropic-byok::2024-10-22::claude-3.5-sonnet"`) for `"chat"`. Requests are sent directly to the Anthropic API as set in the provider override - For `"fastChat"` and `"codeCompletion"`, we use Sourcegraph-provided models via Cody Gateway ## Config examples for various LLM providers Below are configuration examples for setting up various LLM providers using BYOK. These examples are applicable whether or not you are using Sourcegraph-supported models. - In this section, all configuration examples have Sourcegraph-provided models disabled. Please refer to the previous section to use a combination of Sourcegraph-provided models and BYOK. - Ensure that at least one model is available for each Cody feature ("chat" and "autocomplete"), regardless of the provider and model overrides configured. To verify this, [view the configuration](/cody/enterprise/model-configuration#view-configuration) and confirm that appropriate models are listed in the `"defaultModels"` section. ```json { "cody.enabled": true, "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "anthropic", "displayName": "Anthropic", "serverSideConfig": { "type": "anthropic", "accessToken": "token", "endpoint": "https://api.anthropic.com/v1/messages" } } ], "modelOverrides": [ { "modelRef": "anthropic::2024-10-22::claude-3-7-sonnet-latest", "displayName": "Claude 3.7 Sonnet", "modelName": "claude-3-7-sonnet-latest", "capabilities": ["chat"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 132000, "maxOutputTokens": 8192 }, }, { "modelRef": "anthropic::2024-10-22::claude-3-7-sonnet-extended-thinking", "displayName": "Claude 3.7 Sonnet Extended Thinking", "modelName": "claude-3-7-sonnet-latest", "capabilities": ["chat", "reasoning"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 93000, "maxOutputTokens": 64000 }, "reasoningEffort": "low" }, { "modelRef": "anthropic::2024-10-22::claude-3-5-haiku-latest", "displayName": "Claude 3.5 Haiku", "modelName": "claude-3-5-haiku-latest", "capabilities": ["autocomplete", "edit", "chat"], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 132000, "maxOutputTokens": 8192 }, } ], "defaultModels": { "chat": "anthropic::2024-10-22::claude-3-7-sonnet-latest", "fastChat": "anthropic::2024-10-22::claude-3-5-haiku-latest", "codeCompletion": "anthropic::2024-10-22::claude-3-5-haiku-latest" } } ``` In the configuration above, - Set up a provider override for Anthropic, routing requests for this provider directly to the specified Anthropic endpoint (bypassing Cody Gateway) - Add three Anthropic models: - `"anthropic::2024-10-22::claude-3-7-sonnet-latest"` with chat, vision, and tools capabilities - `"anthropic::2024-10-22::claude-3-7-sonnet-extended-thinking"` with chat and reasoning capabilities (note: to enable [Claude's extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) model override should include "reasoning" capability and have "reasoningEffort" defined) - `"anthropic::2024-10-22::claude-3-5-haiku-latest"` with autocomplete, edit, chat, and tools capabilities - Set the configured models as default models for Cody features in the `"defaultModels"` field ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "fireworks", "displayName": "Fireworks", "serverSideConfig": { "type": "fireworks", "accessToken": "token", "endpoint": "https://api.fireworks.ai/inference/v1/completions" } } ], "modelOverrides": [ { "modelRef": "fireworks::v1::mixtral-8x7b-instruct", "displayName": "Mixtral 8x7B", "modelName": "accounts/fireworks/models/mixtral-8x7b-instruct", "capabilities": ["chat"], "category": "other", "status": "stable", "contextWindow": { "maxInputTokens": 7000, "maxOutputTokens": 4000 } }, { "modelRef": "fireworks::v1::starcoder-16b", "modelName": "accounts/fireworks/models/starcoder-16b", "displayName": "(Fireworks) Starcoder 16B", "contextWindow": { "maxInputTokens": 8192, "maxOutputTokens": 4096 }, "capabilities": ["autocomplete"], "category": "balanced", "status": "stable" } ], "defaultModels": { "chat": "fireworks::v1::mixtral-8x7b-instruct", "fastChat": "fireworks::v1::mixtral-8x7b-instruct", "codeCompletion": "fireworks::v1::starcoder-16b" } } ``` In the configuration above, - Set up a provider override for Fireworks, routing requests for this provider directly to the specified Fireworks endpoint (bypassing Cody Gateway) - Add two Fireworks models: - `"fireworks::v1::mixtral-8x7b-instruct"` with "chat" capabiity - used for "chat" and "fastChat" - `"fireworks::v1::starcoder-16b"` with "autocomplete" capability - used for "codeCompletion" ```json "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "openai", "displayName": "OpenAI", "serverSideConfig": { "type": "openai", "accessToken": "token", "endpoint": "https://api.openai.com" } } ], "modelOverrides": [ { "modelRef": "openai::unknown::gpt-4o", "displayName": "GPT-4o", "modelName": "gpt-4o", "capabilities": ["chat"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } }, { "modelRef": "openai::unknown::gpt-4.1-nano", "displayName": "GPT-4.1-nano", "modelName": "gpt-4.1-nano", "capabilities": ["edit", "chat", "autocomplete"], "category": "speed", "status": "stable", "tier": "free", "contextWindow": { "maxInputTokens": 77000, "maxOutputTokens": 16000 } }, { "modelRef": "openai::unknown::o3", "displayName": "o3", "modelName": "o3", "capabilities": ["chat", "reasoning"], "category": "accuracy", "status": "stable", "tier": "pro", "contextWindow": { "maxInputTokens": 68000, "maxOutputTokens": 100000 }, "reasoningEffort": "medium" } ], "defaultModels": { "chat": "openai::unknown::gpt-4o", "fastChat": "openai::unknown::gpt-4.1-nano", "codeCompletion": "openai::unknown::gpt-4.1-nano" } } ``` In the configuration above, - Set up a provider override for OpenAI, routing requests for this provider directly to the specified OpenAI endpoint (bypassing Cody Gateway) - Add three OpenAI models: - `"openai::2024-02-01::gpt-4o"` with chat capability - used as a default model for chat - `"openai::unknown::gpt-4.1-nano"` with chat, edit and autocomplete capabilities - used as a default model for fast chat and autocomplete - `"openai::unknown::o3"` with chat and reasoning capabilities - o-series model that supports thinking, can be used for chat (note: to enable thinking, model override should include "reasoning" capability and have "reasoningEffort" defined). ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "azure-openai", "displayName": "Azure OpenAI", "serverSideConfig": { "type": "azureOpenAI", "accessToken": "token", "endpoint": "https://acme-test.openai.azure.com/", "user": "", "useDeprecatedCompletionsAPI": true } } ], "modelOverrides": [ { "modelRef": "azure-openai::unknown::gpt-4o", "displayName": "GPT-4o", "modelName": "gpt-4o", "capabilities": ["chat"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } }, { "modelRef": "azure-openai::unknown::gpt-4.1-nano", "displayName": "GPT-4.1-nano", "modelName": "gpt-4.1-nano", "capabilities": ["edit", "chat", "autocomplete"], "category": "speed", "status": "stable", "tier": "free", "contextWindow": { "maxInputTokens": 77000, "maxOutputTokens": 16000 } }, { "modelRef": "azure-openai::unknown::o3-mini", "displayName": "o3-mini", "modelName": "o3-mini", "capabilities": ["chat", "reasoning"], "category": "accuracy", "status": "stable", "tier": "pro", "contextWindow": { "maxInputTokens": 68000, "maxOutputTokens": 100000 }, "reasoningEffort": "medium" }, { "modelRef": "azure-openai::unknown::gpt-35-turbo-instruct-test", "displayName": "GPT-3.5 Turbo Instruct", "modelName": "gpt-35-turbo-instruct-test", "capabilities": ["autocomplete"], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 7000, "maxOutputTokens": 4000 } } ], "defaultModels": { "chat": "azure-openai::unknown::gpt-4o", "fastChat": "azure-openai::unknown::gpt-4.1-nano", "codeCompletion": "azure-openai::unknown::gpt-4.1-nano" } } ``` In the configuration above, - Set up a provider override for Azure OpenAI, routing requests for this provider directly to the specified Azure OpenAI endpoint (bypassing Cody Gateway). **Note:** For Azure OpenAI, ensure that the `modelName` matches the name defined in your Azure portal configuration for the model. - Add four OpenAI models: - `"azure-openai::unknown::gpt-4o"` with chat capability - used as a default model for chat - `"azure-openai::unknown::gpt-4.1-nano"` with chat, edit and autocomplete capabilities - used as a default model for fast chat and autocomplete - `"azure-openai::unknown::o3-mini"` with chat and reasoning capabilities - o-series model that supports thinking, can be used for chat (note: to enable thinking, model override should include "reasoning" capability and have "reasoningEffort" defined) - `"azure-openai::unknown::gpt-35-turbo-instruct-test"` with "autocomplete" capability - included as an alternative model - Since `"azure-openai::unknown::gpt-35-turbo-instruct-test"` is not supported on the newer OpenAI `"v1/chat/completions"` endpoint, we set `"useDeprecatedCompletionsAPI"` to `true` to route requests to the legacy `"v1/completions"` endpoint. This setting is unnecessary if you are using a model supported on the `"v1/chat/completions"` endpoint. ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "fireworks", "displayName": "Fireworks", "serverSideConfig": { "type": "openaicompatible", "endpoints": [ { "url": "https://api.fireworks.ai/inference/v1", "accessToken": "token" } ] } }, { "id": "huggingface-codellama", "displayName": "Hugging Face", "serverSideConfig": { "type": "openaicompatible", "endpoints": [ { "url": "https://api-inference.huggingface.co/models/meta-llama/CodeLlama-7b-hf/v1/", "accessToken": "token" } ] } }, ], "modelOverrides": [ { "modelRef": "fireworks::v1::llama-v3p1-70b-instruct", "modelName": "llama-v3p1-70b-instruct", "displayName": "(Fireworks) Llama 3.1 70B Instruct", "contextWindow": { "maxInputTokens": 64000, "maxOutputTokens": 8192 }, "serverSideConfig": { "type": "openaicompatible", "apiModel": "accounts/fireworks/models/llama-v3p1-70b-instruct" }, "clientSideConfig": { "openaicompatible": {} }, "capabilities": ["chat"], "category": "balanced", "status": "stable" }, { "modelRef": "huggingface-codellama::v1::CodeLlama-7b-hf", "modelName": "CodeLlama-7b-hf", "displayName": "(HuggingFace) CodeLlama-7b-hf", "contextWindow": { "maxInputTokens": 8192, "maxOutputTokens": 4096 }, "serverSideConfig": { "type": "openaicompatible", "apiModel": "meta-llama/CodeLlama-7b-hf" }, "clientSideConfig": { "openaicompatible": {} }, "capabilities": ["autocomplete", "chat"], "category": "balanced", "status": "stable" } ], "defaultModels": { "chat": "fireworks::v1::llama-v3p1-70b-instruct", "fastChat": "fireworks::v1::llama-v3p1-70b-instruct", "codeCompletion": "huggingface-codellama::v1::CodeLlama-7b-hf" } } ``` In the configuration above, - Configure two OpenAI-compatible providers: `"fireworks"` and `"huggingface-codellama"` - Add two OpenAI-compatible models: `"fireworks::v1::llama-v3p1-70b-instruct"` and `"huggingface-codellama::v1::CodeLlama-7b-hf"`. Additionally: - Set `clientSideConfig.openaicompatible` to `{}` to indicate to Cody clients that these models are OpenAI-compatible, ensuring the appropriate code paths are utilized - Designate these models as the default choices for chat and autocomplete, respectively ## Disabling legacy completions Available in Sourcegraph 6.4+ and 6.3.2692 By default, Cody will send Autocomplete requests to the legacy OpenAI /completions endpoint (i.e. for pure-inference requests) - if your OpenAI-compatible API endpoint supports only /chat/completions, you may disable the use of the legacy completions endpoint by adding the following above your serverSideConfig endpoints list: ```json "serverSideConfig": { "type": "openaicompatible", "useLegacyCompletions": false, // ^ add this to disable /completions and make Cody only use /chat/completions "endpoints": [ { "url": "https://api-inference.huggingface.co/models/meta-llama/CodeLlama-7b-hf/v1/", "accessToken": "token" } ] } ``` ## Sending custom HTTP headers Available in Sourcegraph v6.4+ and v6.3.2692 By default, Cody will only send an `Authorization: Bearer ` header to OpenAI-compatible endpoints. You may configure custom HTTP headers if you like under the URL of endpoints: ```json "serverSideConfig": { "type": "openaicompatible", "endpoints": [ { "url": "https://api-inference.huggingface.co/models/meta-llama/CodeLlama-7b-hf/v1/", "headers": { "X-api-key": "foo", "My-Custom-Http-Header": "bar" }, // ^ add this to configure custom headers } ] } ``` When using custom headers, both `accessToken` and `accessTokenQuery` configuration settings are ignored. ```json "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "google", "displayName": "Google Gemini", "serverSideConfig": { "type": "google", "accessToken": "token", "endpoint": "https://generativelanguage.googleapis.com/v1beta/models" } } ], "modelOverrides": [ { "modelRef": "google::v1::gemini-1.5-pro", "displayName": "Gemini 1.5 Pro", "modelName": "gemini-1.5-pro", "capabilities": ["chat", "autocomplete"], "category": "balanced", "status": "stable", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 } } ], "defaultModels": { "chat": "google::v1::gemini-1.5-pro", "fastChat": "google::v1::gemini-1.5-pro", "codeCompletion": "google::v1::gemini-1.5-pro" } } ``` In the configuration above, - Set up a provider override for Google Gemini, routing requests for this provider directly to the specified endpoint (bypassing Cody Gateway) - Add the `"google::v1::gemini-1.5-pro"` model, which is used for all Cody features. We do not add other models for simplicity, as adding multiple models is already covered in the examples above ```json "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "google", "displayName": "Google Anthropic", "serverSideConfig": { "type": "google", "accessToken": "token", "endpoint": "https://us-east5-aiplatform.googleapis.com/v1/projects/project-name/locations/us-east5/publishers/anthropic/models" } } ], "modelOverrides": [ { "modelRef": "google::20250219::claude-3-7-sonnet", "displayName": "Claude 3.7 Sonnet", "modelName": "claude-3-7-sonnet@20250219", "capabilities": ["chat", "vision", "tools"], "category": "accuracy", "status": "stable", "contextWindow": { "maxInputTokens": 132000, "maxOutputTokens": 8192 } }, { "modelRef": "google::20250219::claude-3-7-sonnet-extended-thinking", "displayName": "Claude 3.7 Sonnet Extended Thinking", "modelName": "claude-3-7-sonnet@20250219", "capabilities": ["chat", "reasoning"], "category": "accuracy", "status": "stable", "reasoningEffort": "medium", "contextWindow": { "maxInputTokens": 93000, "maxOutputTokens": 64000 } }, { "modelRef": "google::20250219::claude-3-5-haiku", "displayName": "Claude 3.5 Haiku", "modelName": "claude-3-5-haiku@20241022", "capabilities": ["autocomplete", "edit", "chat", "tools"], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 132000, "maxOutputTokens": 8192 } } ], "defaultModels": { "chat": "google::20250219::claude-3.5-sonnet", "fastChat": "google::20250219::claude-3-5-haiku", "codeCompletion": "google::20250219::claude-3-5-haiku" } } ``` In the configuration above, - Set up a provider override for Google Anthropic, routing requests for this provider directly to the specified endpoint (bypassing Cody Gateway) - Add three Anthropic models: - `"google::unknown::claude-3-7-sonnet"` with chat, vision, and tools capabilities - `"google::unknown::claude-3-7-sonnet-extended-thinking"` with chat and reasoning capabilities (note: to enable [Claude's extended thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) model override should include "reasoning" capability and have "reasoningEffort" defined) - `"google::unknown::claude-3-5-haiku"` with autocomplete, edit, chat, and tools capabilities - Set the configured models as default models for Cody features in the `"defaultModels"` field ```json "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "google", "displayName": "Google Gemini", "serverSideConfig": { "type": "google", "accessToken": "token", "endpoint": "https://us-east5-aiplatform.googleapis.com/v1/projects/project-name/locations/us-east5/publishers/anthropic/models" } } ], "modelOverrides": [ { "modelRef": "google::unknown::claude-3-5-sonnet", "displayName": "Claude 3.5 Sonnet (via Google Vertex)", "modelName": "claude-3-5-sonnet@20240620", "contextWindow": { "maxInputTokens": 45000, "maxOutputTokens": 4000 }, "capabilities": ["chat"], "category": "accuracy", "status": "stable" }, { "modelRef": "google::unknown::claude-3-haiku", "displayName": "Claude 3 Haiku", "modelName": "claude-3-haiku@20240307", "capabilities": ["autocomplete", "chat"], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 7000, "maxOutputTokens": 4000 } }, ], "defaultModels": { "chat": "google::unknown::claude-3-5-sonnet", "fastChat": "google::unknown::claude-3-5-sonnet", "codeCompletion": "google::unknown::claude-3-haiku" } } ``` In the configuration above, - Set up a provider override for Google Anthropic, routing requests for this provider directly to the specified endpoint (bypassing Cody Gateway) - Add two Anthropic models: - `"google::unknown::claude-3-5-sonnet"` with "chat" capabiity - used for "chat" and "fastChat" - `"google::unknown::claude-3-haiku"` with "autocomplete" capability - used for "codeCompletion" ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": null, "providerOverrides": [ { "id": "aws-bedrock", "displayName": "Anthropic models through AWS Bedrock", "serverSideConfig": { "type": "awsBedrock", "accessToken": ":", "endpoint": "", "region": "us-west-2" } } ], "modelOverrides": [ { "modelRef": "aws-bedrock::2025-02-19::claude-3-7-sonnet", "displayName": "Claude 3.7 Sonnet", "modelName": "anthropic.claude-3-7-sonnet-20250219-v1:0", "serverSideConfig": { "type": "awsBedrockProvisionedThroughput", "arn": "" // e.g., arn:aws:bedrock:us-west-2:537452198621:provisioned-model/57z3lgkt1cx2 }, "contextWindow": { "maxInputTokens": 132000, "maxOutputTokens": 8192 }, "capabilities": ["chat", "autocomplete"], "category": "balanced", "status": "stable" }, ], "defaultModels": { "chat": "aws-bedrock::2025-02-19::claude-3-7-sonnet", "codeCompletion": "aws-bedrock::2025-02-19::claude-3-7-sonnet", "fastChat": "aws-bedrock::2025-02-19::claude-3-7-sonnet" }, } ``` In the configuration described above, - Set up a provider override for Amazon Bedrock, routing requests for this provider directly to the specified endpoint, bypassing Cody Gateway - Add the `"aws-bedrock::2024-02-29::claude-3-sonnet"` model, which is used for all Cody features. We do not add other models for simplicity, as adding multiple models is already covered in the examples above - Since the model in the example uses [Amazon Bedrock provisioned throughput](https://docs.aws.amazon.com/bedrock/latest/userguide/prov-throughput.html), specify the ARN in the `serverSideConfig.arn` field of the model override. Provider override `serverSideConfig` fields: | **Field** | **Description** | | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `type` | Must be `"awsBedrock"`. | | `accessToken` | Leave empty to rely on instance role bindings or other AWS configurations in the frontend service. Use `:` for direct credential configuration, or `::` if a session token is also required. | | `endpoint` | For pay-as-you-go, set it to an AWS region code (e.g., `us-west-2`) when using a public Amazon Bedrock endpoint. For provisioned throughput, set it to the provisioned VPC endpoint for the bedrock-runtime API (e.g., `https://vpce-0a10b2345cd67e89f-abc0defg.bedrock-runtime.us-west-2.vpce.amazonaws.com`). | | `region` | The region to use when configuring API clients. The `AWS_REGION` Environment variable must also be configured in the `sourcegraph-frontend` container to match. | Provisioned throughput for Amazon Bedrock models can be configured using the `"awsBedrockProvisionedThroughput"` server-side configuration type. Refer to the [Model Overrides](/cody/enterprise/model-configuration#model-overrides) section for more details. If using [IAM roles for EC2 / instance role binding](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html), you may need to increase the [HttpPutResponseHopLimit ](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_InstanceMetadataOptionsRequest.html#:~:text=HttpPutResponseHopLimit) instance metadata option to a higher value (e.g., 2) to ensure that the metadata service can be accessed from the frontend container running in the EC2 instance. See [here](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-IMDS-existing-instances.html) for instructions.
# Cody Enterprise features

Along with the core features, Cody Enterprise offers additional features to enhance your coding experience.

## IDE token expiry Site administrators can set the duration of access tokens for users connecting Cody from their IDEs (VS Code, JetBrains, etc.). This can be configured from the **Site admin** page of the Sourcegraph Enterprise instance. Available options include **7, 14, 30, 60, and 90 days**. ![ide-token-expiry](https://storage.googleapis.com/sourcegraph-assets/Docs/token-expiry.png) ## Guardrails Guardrails for public code is currently in Beta and is supported with VS Code, JetBrains IDEs extensions and Sourcegraph Web app. Open source attribution guardrails for public code, commonly called copyright guardrails, reduce the exposure to copyrighted code. This involves implementing a verification mechanism within Cody to ensure that any code generated by the platform does not replicate open source code. Guardrails for public code are available to all Sourcegraph Enterprise instances and are **disabled** by default. You can enable them from the Site configuration section by setting `attribution.enabled` to `true`. Guardrails don't differentiate between license types. It matches any code snippet that is at least **ten lines** long from the **290,000** indexed open source repositories. ### Guardrails `enforced` check You can `enforce` a Guardrails check to prevent any matching code from being shown to the user. To do so, site admins need to add `"attribution.mode": "enforced"` in the **Site configuration** setting. This will configure the settings for Cody IDE extensions VS Code (v1.82+) or JetBrains (v7.82+) accordingly and will enforce not to display code until attribution checks have finished. ## Admin controls Admin controls are supported with VS Code and JetBrains IDE extension. Site administrators have selective control over users' access to Cody Enterprise, which is managed via the Sourcegraph role-based access control system. This provides a more intuitive user interface for assigning permission to use Cody. ## Analytics Cody Analytics are supported with VS Code IDE extension and on the latest versions of JetBrains IDEs. Cody Enterprise users can view analytics for their instance. A separately managed cloud service for Cody analytics handles user auth, gets metrics data from Sourcegraph's BigQuery instance, and visualizes the metrics data. The following metrics are available for Cody Enterprise users: | **Metric Type** | **What is measured?** | | --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Active users | - Total active users
- Average daily users
- Average no. of days each user used Cody (of last 30 days)
- Cody users by day (last 30 days)
- Cody users by month (last two months)
- Cody users by number of days used | | Completions | - Total accepted completions
- Minutes saved per completion
- Hours saved by completions
- Cody completions by day
- Completions acceptance rate
- Weighted completions acceptance rate
- Average completion latency
- Acceptance rate by language
| | Chat | - Total chat events
- Minutes saved per chat
- Hours saved by chats
- Cody chats by day | | Commands | - Total command events
- Minutes saved per command
- Hours saved by commands
- Cody commands by day
- Most used commands | To enable Cody Analytics: - Create an account on [Sourcegraph Accounts](https://accounts.sourcegraph.com/) - A user already having a Sourcegraph.com account gets automatically migrated to Sourcegraph Accounts. Users can sign in to Cody Analytics using their email and password - Users without a Sourcegraph.com account should contact one of our team members. They can help with both the account setup and assigning instances to specific users - Map your user account to a Sourcegraph instance, and this gives you access to Cody analytics ## Multi-repository context Cody supports multi-repository context, allowing you to search up to 10 repositories simultaneously for relevant information. Open a new chat, type `@`, and select `Remote Repositories.` Keep @-mentioning repos that you want to include in your context. This flexibility lets you get more comprehensive and accurate responses by leveraging information across multiple codebases. ## @-mention context Users can `@-mention` repositories, files, symbols, and web URLs to better support Enterprise teams working with large codebases when chatting with Cody. This helps you define more specific context. To do this, type `@` in the chat, and then select the context of your choice. ![@-mention-directory](https://storage.googleapis.com/sourcegraph-assets/Docs/%40-mention-ent-0525.png) If you want to include recent changes that haven't been indexed in your Sourcegraph instance, you can `@-mention` specific files, lines of code, or symbols. ## Prompt pre-instructions Prompt pre-instructions is supported for Sourcegraph `v5.10` and more. **Site admins** can add and configure prompt pre-instructions for Cody. These are text instructions that Cody uses with every chat query, allowing organizations to configure how Cody responds to their users. For example, if you don’t want Cody to answer questions relating to sensitive non-code matters, you can pre-instruct Cody about it. In this case, if a user asks an unrelated question, Cody responds with a pre-instructed context. To configure pre-instructions, add the following to your site admin configuration file: ```json { ... "modelConfiguration": { "systemPreInstruction": "If the question is not directly related to software development, respond with \"I can only answer programming-related questions\"" } } ``` We cannot guarantee that these pre-instructions or their intended use case will be fully accurate. If your pre-instructions are not working as expected, please get in touch with us. ## Supported LLM models Sourcegraph Enterprise supports different LLM providers and models, such as models from Anthropic and OpenAI. You can do this by adjusting your Sourcegraph instance configuration. For the supported LLM models listed above, refer to the following notes: 1. Microsoft Azure is planning to deprecate the APIs used in Sourcegraph version `>5.3.3` on July 1, 2024 [Source](https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation) 2. Claude 2.1 is not recommended 3. Sourcegraph doesn’t recommend using the GPT-4 (non-Turbo), Claude 1, or Claude 2 models anymore 4. Only supported through legacy completions API 5. BYOK (Bring Your Own Key) with managed services are only supported for Self-hosted Sourcegraph instances 6. GPT-4 and GPT-4o for completions have a bug that is resulting in many failed completions {/*Temporarily removed*/} {/* ## Supported model configuration Use the drop-down menu to make your desired selection and get a detailed breakdown of the supported model configuration for each provider on Cody Enterprise. This is an on-site configuration. Admins should pick a value from the table for `chatModel` to configure their chat model. For the supported LLM model configuration listed above, refer to the following notes: 1. Microsoft Azure is planning to deprecate the APIs used in Sourcegraph version `>5.3.3` on July 1, 2024 [Source](https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation) 2. Claude 2.1 is not recommended 3. Sourcegraph doesn't recommend GPT-4 non-turbo, Claude 1 or 2 models 4. Only supported through legacy completions API 5. BYOK (Bring Your Own Key) with managed services are only supported for Self-hosted Sourcegraph instances */}
# Completions Configuration

Learn how to configure Cody via `completions` on a Sourcegraph Enterprise instance.

Configuring Cody via `completions` is legacy but it's still supported. We recommend using the new [`modelConfiguration`](/cody/enterprise/model-configuration) for flexible LLM model selection. [Cody Enterprise](https://sourcegraph.com/enterprise) supports many models and model providers. You can configure Cody Enterprise to access models via Sourcegraph Cody Gateway or directly using your own model provider account or infrastructure. Let's look at these options in more detail. ## Using Sourcegraph Cody Gateway This is the recommended way to configure Cody Enterprise. It supports all the latest models from Anthropic, OpenAI, and more without requiring a separate account or incurring separate charges. You can learn more about these in our [supported models](/cody/capabilities/supported-models) docs. ## Using your organization's account with a model provider ### Example: Your organization's Anthropic account First, [create your own key with Anthropic](https://console.anthropic.com/account/keys). Once you have the key, go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, "completions": { "provider": "anthropic", "chatModel": "claude-2.0", // Or any other model you would like to use "fastChatModel": "claude-instant-1.2", // Or any other model you would like to use "completionModel": "claude-instant-1.2", // Or any other model you would like to use "accessToken": "" } } ``` ### Example: Your organization's OpenAI account First, [create your own key with OpenAI](https://beta.openai.com/account/api-keys). Once you have the key, go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, "completions": { "provider": "openai", "chatModel": "gpt-4", // Or any other model you would like to use "fastChatModel": "gpt-3.5-turbo", // Or any other model you would like to use "completionModel": "gpt-3.5-turbo-instruct", // Or any other model that supports the legacy completions endpoint "accessToken": "" } } ``` [Learn more about OpenAI models.](https://platform.openai.com/docs/models) ## Using your organization's public cloud infrastructure ### Example: Use Amazon Bedrock You can use Anthropic Claude models on [Amazon Bedrock](https://aws.amazon.com/bedrock/). First, make sure you can access Amazon Bedrock. Then, request access to the Anthropic Claude models in Bedrock. This may take some time to provision. Next, create an IAM user with programmatic access in your AWS account. Depending on your AWS setup, different ways may be required to provide access. All completion requests are made from the `frontend` service, so this service needs to be able to access AWS. You can use instance role bindings or directly configure the IAM user credentials in the configuration. The `AWS_REGION` environment variable must also be set in the `frontend` container to scope the IAM credentials for the AWS region hosting the Bedrock endpoint. Once ready, go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, "completions": { "provider": "aws-bedrock", "chatModel": "anthropic.claude-3-opus-20240229-v1:0", "completionModel": "anthropic.claude-instant-v1", "endpoint": "", "accessToken": "" } } ``` For the `chatModel` and `completionModel` fields, see [Amazon's Bedrock docs](https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids.html) for an up-to-date list of supported model IDs, and cross reference against Sourcegraph's [supported LLM list](/cody/capabilities/supported-models) to verify compatibility with Cody. For `endpoint`, you can either: - For **pay-as-you-go**, set it to an AWS region code (e.g., `us-west-2`) when using a public Amazon Bedrock endpoint - For **provisioned throughput**, set it to the provisioned VPC endpoint for the `bedrock-runtime` API (e.g., `"https://vpce-0a10b2345cd67e89f-abc0defg.bedrock-runtime.us-west-2.vpce.amazonaws.com"`) For `accessToken`, you can either: - Leave it empty and rely on instance role bindings or other AWS configurations in the `frontend` service - Set it to `:` if directly configuring the credentials - Set it to `::` if a session token is also required #### AWS Bedrock: Latency optimization Optimization for latency with AWS Bedrock is available in Sourcegraph v6.5 and more. AWS Bedrock supports [Latency Optimized Inference](https://docs.aws.amazon.com/bedrock/latest/userguide/latency-optimized-inference.html) which can reduce autocomplete latency with models like Claude 3.5 Haiku by up to ~40%. To use Bedrock's latency optimized inference feature for a specific model with Cody, configure the `"latencyOptimization": "optimized"` setting under the `serverSideConfig` of any model in `modelOverrides`. For example: ```json "modelOverrides": [ { "modelRef": "aws-bedrock::v1::claude-3-5-haiku-latency-optimized", "modelName": "us.anthropic.claude-3-5-haiku-20241022-v1:0", "displayName": "Claude 3.5 Haiku (latency optimized)", "capabilities": [ "chat", "autocomplete" ], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 200000, "maxOutputTokens": 4096 }, "serverSideConfig": { "type": "awsBedrock", "latencyOptimization": "optimized" } }, { "modelRef": "aws-bedrock::v1::claude-3-5-haiku", "modelName": "us.anthropic.claude-3-5-haiku-20241022-v1:0", "displayName": "Claude 3.5 Haiku", "capabilities": [ "chat", "autocomplete" ], "category": "speed", "status": "stable", "contextWindow": { "maxInputTokens": 200000, "maxOutputTokens": 4096 }, "serverSideConfig": { "type": "awsBedrock", "latencyOptimization": "standard" } } ] ``` See also [Debugging: running a latency test](#debugging-running-a-latency-test). ### Example: Using GCP Vertex AI On [GCP Vertex](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude), we only support Anthropic Claude models. - Enable the [Vertex AI API](https://console.cloud.google.com/marketplace/product/google/aiplatform.googleapis.com) in the GCP console. Once Vertex has been enabled in your project, navigate to the [Vertex Model Garden](https://console.cloud.google.com/vertex-ai/model-garden) to select and enable the Anthropic Claude model(s) that you wish to use with Cody. See [Supported LLM Models](/capabilities/supported-models) for an up-to-date list of Anthropic Claude models supported by Cody. It may take some time to enable Vertex and provision access to the models you plan to use 1. **Create a Service Account**: - Create a [service account](https://cloud.google.com/iam/docs/service-account-overview) - Assign the `Vertex AI User` role to the service account - Generate a JSON key for the service account and download it 2. **Convert JSON Key to Base64** by doing: ```python cat | base64 ``` Once ready, go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, "completions": { "chatModel": "claude-3-opus@20240229", "completionModel": "claude-3-haiku@20240307", "provider": "google", "endpoint": "", "accessToken": "" } } ``` For the `endpoint`, you can: - Go to the Claude 3 Haiku docs on the GCP Console Model garden - Scroll through the page to find the example that shows how to use the `cURL` command with the Claude 3 Haiku model. The example will include a sample request JSON body and the necessary endpoint URL. Copy the URL in the site-admin config - The endpoint URL will look something like this: `https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/` - Example URL: `https://us-east5-aiplatform.googleapis.com/v1/projects/sourcegraph-vertex-staging/locations/us-east5/publishers/anthropic/models` ### Example: Use Azure OpenAI service Create a project in the Azure OpenAI Service portal. From the project overview, go to **Keys and Endpoint and get one of the keys** on that page and the **endpoint**. Next, under **Model deployments**, click **manage deployments** and ensure you deploy the models you want, for example, `gpt-35-turbo`. Take note of the **deployment name**. Once done, go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, "completions": { "provider": "azure-openai", "chatModel": "", "fastChatModel": "", "completionModel": "", // the model must support the legacy completions endpoint such as gpt-3.5-turbo-instruct "endpoint": "", "accessToken": "" } } ``` For the access token, - For Sourcegraph `v5.2.4` or more, the access token can be left empty, and it will rely on Environmental, Workload Identity, or Managed Identity credentials configured for the `frontend` and `worker` services - Set it to `` if directly configuring the credentials using the API key specified in the Azure portal ### Use StarCoder for autocomplete When tested with other coder models for the autocomplete use case, [StarCoder](https://huggingface.co/blog/starcoder) offered significant improvements in quality and latency compared to our control groups for users on Sourcegraph.com. You can read more about the improvements in our [October 2023 release notes](https://sourcegraph.com/blog/feature-release-october-2023) and the [GA release notes](https://sourcegraph.com/blog/cody-is-generally-available). To ensure a fast and reliable experience, we are partnering with [Fireworks](https://fireworks.ai/) and have set up a dedicated hardware deployment for our Enterprise users. Sourcegraph supports StarCoder using the [Cody Gateway](/cody/core-concepts/cody-gateway). To enable StarCoder, go to **Site admin > Site configuration** (`/site-admin/configuration`) and change the `completionModel`: ```json { // [...] "cody.enabled": true, "completions": { "provider": "sourcegraph", "completionModel": "fireworks/starcoder" } } ``` Users of the Cody extensions will automatically pick up this change when connected to your Enterprise instance. ## Debugging: Running a latency test Debugging latency optimizated inference is supported in Sourcegraph v6.5 and more. Site administrators can test completions latency by sending a special debug command in any Cody chat window (in the web, in the editor, etc.): ```shell cody_debug:::{"latencytest": 100} ``` Cody will then perform `100` quick `Hello, please respond with a short message.` requests to the LLM model selected in the dropdown, and measure the time taken to get the first streaming event back (for example first token from the model.) It records all of these requests timing information, and then responds with a report indicating the latency between the Sourcegraph `frontend` container and the LLM API: ```shell Starting latency test with 10 requests... Individual timings: [... how long each request took ...] Summary: * Requests: 10/10 successful * Average: 882ms * Minimum: 435ms * Maximum: 1.3s ``` This can be helpful to get a feel for the latency of particular models, or models with different configurations - such as when using the AWS Bedrock Latency Optimized Inference feature. Few important considerations: - Debug commands are only available to site administrators and have no effect when used by regular users. - Sourcegraph's built-in Grafana monitoring also has a full `Completions` dashboard for monitoring LLM requests, performance, etc.
# Cody Input and Output Token Limits

Learn about Cody's token limits and how to manage them.

For all models, Cody allows up to **4,000 tokens of output**, which is approximately **500-600** lines of code. For Claude 3 Sonnet models, Cody tracks two separate token limits: - The @-mention context is limited to **30,000 tokens** (~4,000 lines of code) and can be specified using the @-filename syntax. The user explicitly defines this context, which provides specific information to Cody. - Conversation context is limited to **15,000 tokens**, including user questions, system responses, and automatically retrieved context items. Apart from user questions, Cody generates this context automatically. All other models are currently capped at **7,000 tokens** of shared context between the `@-mention` context and chat history. Here's a detailed breakdown of the token limits by model: | **Model** | **Conversation Context** | **@-mention Context** | **Output** | | ----------------------------- | ------------------------ | --------------------- | ---------- | | GPT 4o mini | 7,000 | shared | 4,000 | | GPT o3 mini medium | 7,000 | shared | 4,000 | | Claude 3.5 Haiku | 7,000 | shared | 4,000 | | Claude 3.5 Sonnet (New) | 15,000 | 30,000 | 4,000 | | **Claude Sonnet 4** | **15,000** | **45,000** | **4,000** | | Gemini 1.5 Pro | 7,000 | shared | 4,000 | | Gemini 2.0 Flash | 7,000 | shared | 4,000 | | Gemini 2.0 Flash-Lite Preview | 7,000 | shared | 4,000 | The Pro tier supports the token limits for the LLM models on Free tier, plus: | **Model** | **Conversation Context** | **@-mention Context** | **Output** | | ------------------------------ | ------------------------ | --------------------- | ---------- | | GPT 4o mini | 7,000 | shared | 4,000 | | GPT o3 mini medium | 7,000 | shared | 4,000 | | GPT 4 Turbo | 7,000 | shared | 4,000 | | GPT 4o | 7,000 | shared | 4,000 | | o1 | 7,000 | shared | 4,000 | | Claude 3.5 Haiku | 7,000 | shared | 4,000 | | Claude 3.5 Sonnet (New) | 15,000 | 30,000 | 4,000 | | **Claude Sonnet 4 w/Thinking** | **15,000** | **45,000** | **4,000** | | Claude 3.7 Sonnet | 15,000 | 30,000 | 4,000 | | Gemini 1.5 Pro | 15,000 | 30,000 | 4,000 | | Gemini 2.0 Flash | 7,000 | shared | 4,000 | | Gemini 2.0 Flash-Lite Preview | 7,000 | shared | 4,000 | The Enterprise tier supports the token limits for the LLM models on Free and Pro tier, plus: | **Model** | **Conversation Context** | **@-mention Context** | **Output** | | ------------------------------ | ------------------------ | --------------------- | ---------- | | GPT 4o mini | 7,000 | shared | 4,000 | | GPT o3 mini medium | 7,000 | shared | 4,000 | | GPT 4 Turbo | 7,000 | shared | 4,000 | | GPT 4o | 7,000 | shared | 4,000 | | o1 | 7,000 | shared | 4,000 | | o3 mini high | 7,000 | shared | 4,000 | | Claude 3.5 Haiku | 7,000 | shared | 4,000 | | Claude 3.5 Sonnet (New) | 15,000 | 30,000 | 4,000 | | **Claude Sonnet 4 w/Thinking** | **15,000** | **45,000** | **4,000** | | **Claude Opus 4** | **15,000** | **45,000** | **4,000** | | **Claude Opus 4 w/Thinking** | **15,000** | **45,000** | **4,000** | | Claude 3.7 Sonnet | 15,000 | 30,000 | 4,000 | | Gemini 2.0 Flash | 7,000 | shared | 4,000 | | Gemini 2.0 Flash-Lite Preview | 7,000 | shared | 4,000 |
For Cody Enterprise, the token limits are the standard limits. Exact token limits may vary depending on your deployment. Please get in touch with your Sourcegraph representative. For more information on how Cody builds context, see our [docs here](/cody/core-concepts/context). ## What is a Context Window? A context window in large language models refers to the maximum number of tokens (words or subwords) the model can process simultaneously. This window determines how much context the model can consider when generating text or code. Context windows exist due to computational limitations and memory constraints. Large language models have billions of parameters, and processing extremely long sequences of text can quickly become computationally expensive and memory-intensive. Limiting the context window allows the model to operate more efficiently and make predictions in a reasonable amount of time. ## What is an Output Limit? **Output Limit** refers to the maximum number of tokens a large language model can generate in a single response. This limit is typically set to ensure the model's output remains manageable and relevant to the context. When a model generates text or code, it does so token by token, predicting the most likely next token based on the input context and its learned patterns. The output limit determines when the model should stop generating further tokens, even if it could continue. The output limit helps to keep the generated text focused, concise, and manageable by preventing the model from going off-topic or generating excessively long responses. It also ensures that the output can be efficiently processed and displayed by downstream applications or user interfaces while managing computational resources. ## Tradeoffs: Size, Accuracy, Latency and Cost So why does Cody not use each model's full available context window? We need to consider a few tradeoffs, namely, context size, retrieval accuracy, latency, and costs. ### Context Size A larger context window allows Cody to consider more information, potentially leading to more coherent and relevant outputs. However, in RAG-based systems like Cody, the value of increasing the context window is related to the precision and recall of the underlying retrieval mechanism. If the relevant files can be retrieved with high precision and added to an existing context window, expansion may not increase response quality. Conversely, some queries require a vast array of documents to synthesize the best response, so increasing the context window would be beneficial. We work to balance these nuances against increased latency and cost tradeoffs for input token lengths. ### Retrieval Accuracy Not all context windows are created equal. Research shows that an LLM's ability to retrieve a fact from a context window can degrade dramatically as the size of the context window increases. This means it is important to put the relevant information into as few tokens as possible to avoid confusing the underlying LLM. As foundation models continue to improve, we see an increase in context retrieval, meaning that large context windows are becoming more viable. We are excited to bring these improvements to Cody. ### Latency With a larger context window, the model needs to process more data, which can increase the latency or response time. The end user often experiences this as "time to first token" or how long they wait until they see an output start to stream. In some cases, longer latency is a worthy tradeoff for higher accuracy, but our research shows that this is very use-case and user-dependent. ### Computational Cost Finally, processing large context windows costs linearly with the context window size. Cody leverages our expertise in code-based RAG to drive down generation costs while maintaining output quality to provide a high-quality response at a reasonable cost to the user.
# Keyword Search

Learn how Cody makes use to Keyword Search to gather context.

Keyword search is the traditional approach to text search. It splits content into terms and builds a mapping from terms to documents. At query time, it extracts terms from the query and uses the mapping to retrieve your documents. Both Cody chat and completions use Keyword Search. It comes out of the box without any additional setup. Cody with Keyword Search searches your local VS Code workspace and is a cost-effective and time-saving solution. For an enterprise admin who has set up Cody with a Code Search instance, developers on their local machines can seamlessly access it.
# Core Concepts

Learn about all the core concepts and fundamentals that helps Cody provide codebase-aware answers.

# Cody Enterprise architecture

[Cody Enterprise](/cody/clients/enable-cody-enterprise) can be deployed via the Sourcegraph Cloud or on your self-hosted infrastructure. This page describes the architecture diagrams for Cody deployed in different Sourcegraph environments.

{/* Figma source: https://www.figma.com/file/lAPHpdhtEmOJ22IQXVZ0vs/Cody-architecture-diagrams-SQS-draft-2024-04?type=whiteboard&node-id=0-1&t=blg78H2YXXbdGSPc-0 */} ## Sourcegraph Cloud deployment This is a recommended deployment for Cody Enterprise. It uses the Sourcegraph Cloud infrastructure and Cody gateway. ## Sourcegraph Enterprise Server (self-hosted) on Amazon Bedrock This is an example of a more complex deployment that uses Sourcegraph Enterprise Server (self-hosted) and Amazon Bedrock. ## Data flow The following diagram describes the data flow between the different components of Cody Enterprise.
# Cody Context

Understand how context helps Cody write more accurate code.

Context refers to any additional information provided to help Cody understand and write code relevant to your codebase. While LLMs have extensive knowledge, they lack context about an individual or organization's codebase. Cody's ability to provide context-aware code responses is what sets it apart. ## Why is context important? Context and [methods of retrieving context](#context-sources) are crucial to the quality and accuracy of AI. Cody relies on its ability to retrieve context from user codebases to provide reliable and accurate answers to developers’ questions. When Cody has access to the most relevant context about your codebase, it can: - Answer questions about your codebase - Produce unit tests and docs - Generate code that aligns with the libraries and style of your codebase - Significantly reduce your work that's required to translate LLM-provided answers into actionable value for your users ## Context sources Cody uses a variety of sources to retrieve context relevant to the user input. These sources include: - **Keyword Search**: A traditional text-based search method that finds keywords matching the user input. When needed, queries are automatically rewritten to include more relevant terms. - **Sourcegraph Search**: The powerful native Sourcegraph Search API. Queries are sent to the SG instance (managed or self-hosted), and search is done using the SG search stack. Relevant documents are returned to the user IDE for use by the LLM. - **Code Graph**: Analyzing the structure of the code, Cody examines how components are interconnected and used, finding context based on code elements' relationships. All these methods collectively ensure Cody's ability to provide relevant and high-quality context to enhance your coding experience. ## Cody context fetching features Cody uses @-mentions to retrieve context from your codebase. Inside the chat window, there is an `@` icon that you can click to select a context source. Alternatively, you can press `@` to open the context picker. Based on your Cody tier, you can @-mention the following: | **Tier** | **Client** | **Files** | **Symbols** | **Web URLs** | **Remote Files/Directories** | **OpenCtx** | | -------------- | ------------- | --------- | ----------- | ------------ | ---------------------------- | ----------- | | **Free/Pro** | VS Code | ✅ | ✅ | ✅ | ❌ | ✅ | | | JetBrains | ✅ | ❌ | ✅ | ❌ | ❌ | | | Visual Studio | ✅ | ✅ | ✅ | ❌ | ❌ | | | Cody Web | ✅ | ✅ | ✅ | ❌ | ❌ | | **Enterprise** | VS Code | ✅ | ✅ | ✅ | ✅ | ✅ | | | JetBrains | ✅ | ❌ | ✅ | ✅ | ❌ | | | Visual Studio | ✅ | ✅ | ✅ | ✅ | ✅ | | | Cody Web | ✅ | ✅ | ✅ | ✅ | ❌ | ## Repo-based context Cody supports repo-based context. You can link single or multiple repositories based on your tier. Here's a detailed breakdown of the number of repositories supported by each client for Cody Free, Pro, and Enterprise users: | **Tier** | **Client** | **Repositories** | | -------------- | ------------- | ---------------- | | **Free/Pro** | VS Code | 1 | | | JetBrains | 1 | | | Visual Studio | 1 | | **Enterprise** | Cody Web | Multi | | | VS Code | Multi | | | JetBrains | Multi | | | Visual Studio | Multi | ## How does context work with Cody prompts? Cody works in conjunction with an LLM to provide codebase-aware answers. The LLM is a machine learning model that generates text in response to natural language prompts. However, the LLM needs to inherently understand your codebase or specific coding requirements. Cody bridges this gap by generating context-aware prompts. A typical prompt has three parts: - **Prefix**: An optional description of the desired output, often derived from predefined [Prompts](/cody/capabilities/commands) that specify tasks the LLM can perform - **User input**: The information provided, including your code query or request - **Context**: Additional information that helps the LLM provide a relevant answer based on your specific codebase ## Impact of context LLM vs Cody When the same prompt is sent to a standard LLM, the response may need more specifics about your codebase. In contrast, Cody augments the prompt with context from relevant code snippets, making the answer far more specific to your codebase. This difference underscores the importance of context in Cody's functionality. ## Manage Cody context window size While Cody aims to provide maximum context for each prompt, there are limits to ensure efficiency. For more details, see our [docs on token limits](/cody/core-concepts/token-limits). Site administrators can update the maximum context window size to meet their specific requirements. While using fewer tokens is a cost-saving solution, it can also cause errors. For example, using an `edit` or `describe` type prompts with a too small context window size might give you errors like `You've selected too much code`. Using more tokens usually produces higher-quality responses but also increases response times. In general, it's recommended not to modify the token limit. However, if needed, you can set it to a value that should not compromise quality or generate errors.
# Sourcegraph Cody Gateway

Learn how Cody Gateway powers the default Sourcegraph provider for completions, enabling Cody features for Sourcegraph Enterprise customers.

Cody Gateway is available for Sourcegraph Enterprise customers using version 5.1 or more. Cody Gateway powers the default `"provider": "sourcegraph"` and Cody completions for Sourcegraph Enterprise users. It integrates with various upstream LLM providers, including [Anthropic](https://www.anthropic.com/) and [OpenAI](https://openai.com/), with rate limits, quotas, and model availability tied to your Sourcegraph Enterprise subscription. ## Supported Models For a full list of supported models and providers, read our [Supported LLMs](/cody/capabilities/supported-models) docs. ## Setting up Cody Gateway in Sourcegraph Enterprise To enable completions provided by Cody Gateway on your Sourcegraph Enterprise instance, make sure your license key is set and Cody is enabled in your [site configuration](/admin/config/site_config): ```jsonc { "licenseKey": "<...>", "cody.enabled": true, "completions": { "provider": "sourcegraph" } } ``` Once the license key is added, default configuration and authentication are automatically applied. For more details about configuring Cody, it's recommended to read about [Enabling Cody for Sourcegraph Enterprise](/cody/clients/enable-cody-enterprise). Cody Gateway is hosted at `cody-gateway.sourcegraph.com`. To use Cody Gateway, your Sourcegraph instance must be connected to the service in this domain. Sourcegraph Cody Gateway access must be included in your Sourcegraph Enterprise subscription. You can verify it by checking it with your account manager. If you are a [Sourcegraph Cloud](/cloud/) user, Cody is enabled by default on your instance starting with Sourcegraph 5.1. ## Rate limits and quotas Rate limits, quotas, and model availability are tied to your Sourcegraph Enterprise license for Sourcegraph Enterprise instances. All successful requests to Cody Gateway will count toward your rate limits. Unsuccessful requests are not counted as usage. Rate limits, quotas, and model availability are also configured per Cody feature - for example, you will have separate rate limits for Cody chat and completions. In addition to the above, we may throttle concurrent requests to Cody Gateway per Sourcegraph Enterprise instance or Cody App user to prevent excessive burst consumption. You can reach out for more details about Sourcegraph Cody Gateway access available to you and how you can gain access to higher rate limits, quotas, and/or model options. ## Privacy and security Sourcegraph Cody Gateway does not retain sensitive data (prompt test and source code included in requests, etc.) from any traffic received. Only rate limit consumption per Sourcegraph Enterprise license and high-level diagnostic data (error codes from upstream, numeric/enum request parameters, etc) are tracked. For more details about Cody Gateway security practices, please contact your account manager. For more privacy details about Cody in general, refer to the [Cody usage and privacy notice](https://about.sourcegraph.com/terms/cody-notice).
# Code Graph

Understand what is Code Graph and how Cody use it to gather context.

Code Graph is a key component of Cody's capacity to generate contextual responses based on your codebase. It involves analyzing the structure of the code rather than treating it as plain text. Cody examines how different components of the codebase are interconnected and how they are used. This method is dependent on the code's structure and inheritance relationships. It can help Cody find context related to your input based on how code elements are linked and utilized. ## Code Graph data Code graph data refers to the information that describes various semantic elements within your source code, like definitions, references, symbols, and doc comments. This dataset is produced by an indexer and subsequently transferred to a Sourcegraph instance. The process of generating this data can vary based on factors such as the programming language and build system in use. In some cases, Sourcegraph can automatically create this data through auto-indexing within the platform itself. Alternately, you may need to set up a periodic CI job, specifically designed to produce and upload this index to your Sourcegraph instance. As of January 23, 2023, open-source projects have the capability to generate Code Graph data in their CI pipelines and then upload it to Sourcegraph.com, enhancing precision in code navigation.
# Installing Cody in VS Code

Learn how to use Cody and its features with the VS Code editor.

The Cody extension by Sourcegraph enhances your coding experience in VS Code by providing intelligent code suggestions, context-aware autocomplete, and advanced code analysis. This guide will walk you through the steps to install and set up Cody within your VS Code environment. ## Prerequisites - You have the latest version of [VS Code](https://code.visualstudio.com/) installed - You have a Free or Pro account via Sourcegraph.com or a Sourcegraph Enterprise account ## Install the VS Code extension You can install Cody extension directly from the [VS Code marketplace](https://marketplace.visualstudio.com/items?itemName=sourcegraph.cody-ai) or by following these steps directly within VS Code: - Open VS Code editor on your local machine - Click the **Extensions** icon in the Activity Bar on the side of VS Code, or use the keyboard shortcut `Cmd+Shift+X` (macOS) or `Ctrl+Shift+X` (Windows/Linux) - Type **Cody AI** in the search bar and click the **Install** button - After installing, you may be prompted to reload VS Code to activate the extension ![install-cody-vscode](https://storage.googleapis.com/sourcegraph-assets/Docs/install-cody-vscode-2025.png) Alternatively, you can also [download and install the extension from the VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=sourcegraph.cody-ai) directly. ## Connect the extension to Sourcegraph After a successful installation, the Cody icon appears in the [Activity sidebar](https://code.visualstudio.com/api/ux-guidelines/activity-bar). ### Cody Free or Cody Pro Users Cody Free and Cody Pro users can sign in to their Sourcegraph.com accounts through GitHub, GitLab, or Google. ![](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-new-ui-2025.jpg) ### Sourcegraph Enterprise Cody Users If you are using an older version of Cody, uninstall it and reload VS Code. It's always recommended to install the latest version before proceeding to the next steps. Sourcegraph Enterprise users should connect Cody to their Enterprise instance by clicking **Sign In to Your Enterprise Instance**. You'll be prompted to choose how to sign-in, Enter the URL of your Enterprise instance. If you are unsure, please contact your administrator. ![](https://storage.googleapis.com/sourcegraph-assets/Docs/vscode-cody-enterprise-login.png) A pop-up will ask if you want to Open the URL in a new window. Click **Open** to open the URL in a new window. Next, sign in to your instance. If you do not yet have a login, please contact your administrator. ![](https://storage.googleapis.com/sourcegraph-assets/Docs/sign-in-to-sourcegraph-2025.jpg) Create an access token from Account Settings - Access Tokens. Click **+ Generate new token** Creating an access token Name the token and click **+ Generate token**. Token name Copy the token and return to VS Code. Copying the token Again, click **Sign In to Your Enterprise Instance** and choose **Sign In to Sourcegraph Instances v5.1 and above**. Enter the URL of your instance. You should now be prompted to authorize Sourcegraph to connect to your VS Code extension using the token you created. Click **Authorize**. Finally, you will be asked to allow the extension access. CLick **Open**. VS Code should now display the Cody panel and you're ready to go. ## Verifying the installation Once connected, click the Cody icon from the sidebar again. The Cody extension will open in a configurable side panel. Let's create an autocomplete suggestion to verify that the Cody extension has been successfully installed and is working as expected. Cody provides intelligent code suggestions and context-aware autocompletions for numerous programming languages like JavaScript, Python, TypeScript, Go, etc. - Create a new file in VS Code, for example, `code.js` - Next, type the following algorithm function to sort an array of numbers ```js function bubbleSort(array){ } ``` - As you start typing, Cody will automatically provide suggestions and context-aware completions based on your coding patterns and the code context - These autocomplete suggestions appear as grayed text. To accept the suggestion, press the `Tab` key ## Chat Cody chat in VS Code is available in a unified interface opened right next to your code. Once connected to Sourcegraph, a new chat input field is opened with a default `@-mention` [context chips](#context-retrieval). All your previous and existing chats are stored for later use and can be accessed via the **History** icon from the top menu. You can download them to share or use later in a `.json` file or delete them altogether. ### Chat interface The chat interface is designed intuitively. Your very first chat input lives at the top of the panel, and the first message in any chat log will stay pinned to the top of the chat. After your first message, the chat input window moves to the bottom of the sidebar. Since your first message to Cody anchors the conversation, you can return to the top chat box anytime, edit your prompt, or re-run it using a different LLM model. ### Chat History A chat history icon at the top of your chat input window allows you to navigate between chats (and search chats) without opening the Cody sidebar. ### Changing LLM model for chat You need to be a Cody Free or Pro user to have multi-model selection capability. You can view which LLMs you have access to on our [supported LLMs page](/cody/capabilities/supported-models). Enterprise users with the new [model configuration](/cody/clients/model-configuration) can use the LLM selection dropdown to choose a chat model. For Chat: - Open chat or toggle between editor and chat (Opt+L/Alt+L) - Click on the model selector (which by default indicates Claude 3.5 Sonnet) - See the selection of models and click the model you desire. This model will now be the default model going forward on any new chats For Edit: - On any file, select some code and a right-click - Select Cody->Edit Code (optionally, you can do this with Opt+K/Alt+K) - Select the default model available - See the selection of models and click the model you desire. This model will now be the default model going forward on any new edits ### Selecting Context with @-mentions Cody's chat allows you to add files and symbols as context in your messages. - Type `@-file` and then a filename to include a file as a context - Type `@#` and then a symbol name to include the symbol's definition as context. Functions, methods, classes, types, etc., are all symbols The `@-file` also supports line numbers to query the context of large files. You can add ranges of large files to your context by @-mentioning a large file and appending a number range to the filename, for example, `@filepath/filename:1-10`. When you `@-mention` files to add to Cody’s context window, the file lookup takes `files.exclude`, `search.exclude`, and `.gitgnore` files into account. This makes the file search faster as a result up to 100ms. Moreover, when you `@-mention` files, Cody will track the number of characters in those files against the context window limit of the selected chat model. As you `@-mention` multiple files, Cody will calculate how many tokens of the context window remain. When the remaining context window size becomes too small, you'll receive **File too large** errors when attempting to `@-mention` additional files. Cody defaults to showing @-mention context chips for all the context it intends to use. When you open a new chat, Cody will show context chips for your current repository and current file (or file selection if you have code highlighted). ### Context retrieval When you start a new Cody chat, the chat input window opens with a default `@-mention` context chips for all the context it intends to use. This context is based on your current repository and current file (or a file selection if you have code highlighted). ![context-retrieval](https://storage.googleapis.com/sourcegraph-assets/Docs/context-retrieval-2025.png) At any point in time, you can edit these context chips or remove them completely if you do not want to use these as context. Any chat without a context chip will instruct Cody to use no codebase context. However, you can always provide an alternate `@-mention` file or symbols to let Cody use it as a new source of context. When you have both a repository and files @-mentioned, Cody will search the repository for context while prioritizing the mentioned files. ### @-mention context providers with OpenCtx OpenCtx context providers are in Experimental stage for all Cody users. Enterprise users can also use this but with limited support. If you have feedback or questions, please visit our [support forum](https://community.sourcegraph.com/c/openctx/10). [OpenCtx](https://openctx.org/) is an open standard for bringing contextual info about code into your dev tools. Cody Free and Pro users can use OpenCtx providers to fetch and use context from the following sources: - [Webpages](https://openctx.org/docs/providers/web) (via URL) - [Jira tickets](https://openctx.org/docs/providers/jira) - [Linear issues](https://openctx.org/docs/providers/linear-issues) - [Notion pages](https://openctx.org/docs/providers/notion) - [Google Docs](https://openctx.org/docs/providers/google-docs) - [Sourcegraph code search](https://openctx.org/docs/providers/sourcegraph-search) To try it out, add context providers to your VS Code settings. For example, to use the [DevDocs provider](https://openctx.org/docs/providers/devdocs), add the following to your `settings.json`: ```json "openctx.providers": { "https://openctx.org/npm/@openctx/provider-devdocs": { "urls": ["https://devdocs.io/go/", "https://devdocs.io/angular~16/"] } }, ``` You don't need the OpenCtx VS Code extension to use context fetching with OpenCtx. We recommend uninstalling the extension before using this feature in Cody. ### Rerun prompts with different context If Cody's answer isn't helpful, you can try asking again with different context: - Public knowledge only: Cody will not use your own code files as context; it’ll only use knowledge trained into the base model. - Add context: Provides @-mention context options to improve the response by explicitly including files, symbols, remote repositories, or even web pages (by URL). ![re-run-with-context](https://storage.googleapis.com/sourcegraph-assets/Docs/re-run-with-context-2025.png) ## Context fetching mechanism VS Code users on the Free or Pro plan use [local context](/cody/core-concepts/context#context-sources). Enterprise users can use the full power of the Sourcegraph search engine as Cody's primary context provider. Read more about [Context fetching mechanism](/cody/core-concepts/context/#context-fetching-mechanism) in detail. ## Context sources You can @-mention files, symbols, and web pages in Cody. Cody Enterprise also supports @-mentioning repositories to search for context in a broader scope. Cody's experimental [OpenCtx](https://openctx.org) support adds even more context sources, including Jira, Linear, Google Docs, Notion, and more. Cody Free and Pro offer single-repo context, and Cody Enterprise supports multi-repo context. ### Cody Context Filters Context Filters is available for all Cody Enterprise users running Cody VS Code extension version `>=1.20.0`. Admins on the Sourcegraph Enterprise instance can use the Cody Context Filters to determine which repositories Cody can use as the context in its requests to third-party LLMs. Inside your site configuration, you can define a set of `include` and `exclude` rules that will be used to filter the list of repositories Cody can access. For repos mentioned in the `exclude` field, prompts are disabled, and you cannot use them for context fetching. If you try running any of these, you'll be prompted with an error message. However, Cody chat will still work, and you can use it to ask questions. [Read more about the Cody Context Filters here →](/cody/capabilities/ignore-context) ## Prompts Cody allows you create quick, ready-to-use [prompts](/cody/capabilities/commands) to automate key tasks in your workflow. Prompts are created and saved in the Prompt Library and can be accessed from the **Tools > Prompt Library** in the top navigation bar in your Sourcegraph instance. To help you get started, there are a few prompts that are available by default. These can assist you to: - Document code - Explain code - Detect code smells - Generate unit tests ![default-prompts](https://storage.googleapis.com/sourcegraph-assets/Docs/default-prompts-2025.png) ## Smart Apply code suggestions Cody lets you dynamically insert code from chat into your files with **Smart Apply**. Every time Cody provides you with a code suggestion, you can click the **Apply** button. Cody will then analyze your open code file, find where that relevant code should live, and add a diff. For chat messages where Cody provides multiple code suggestions, you can apply each in sequence to go from chat suggestions to written code. Smart Apply also supports executing commands in the terminal. When you ask Cody a question related to terminal commands, you can execute the suggestion in your terminal by clicking the `Execute` button in the chat window. ![smart-apply-execute](https://storage.googleapis.com/sourcegraph-assets/Docs/smart-apply-2025.png) ## Keyboard shortcuts Cody provides a set of powerful keyboard shortcuts to streamline your workflow and boost productivity. These shortcuts allow you to quickly access Cody's features without leaving your keyboard. * `Opt+L` (macOS) or `Alt+L` (Windows/Linux): Toggles between the chat view and the last active text editor. If a chat view doesn't exist, it opens a new one. When used with an active selection in a text editor, it adds the selected code to the chat for context. * `Shift+Opt+L` (macOS) or `Shift+Alt+L` (Windows/Linux): Instantly starts a new chat session, perfect for when you want to begin a fresh conversation with Cody. * `Opt+K` (macOS) or `Alt+K` (Windows/Linux): Opens the Edit Code instruction box. This works with either selected code or the code at the cursor position, allowing you to quickly request edits or improvements. * `Opt+C` (macOS) or `Alt+C` (Windows/Linux): Opens the Cody Commands Menu, giving you quick access to a range of Cody's powerful features. * `Cmd+.` (macOS) or `Ctrl+.` (Windows/Linux): Opens the Quick Fix menu, which includes options for Cody to edit or generate code based on your current context. ## Updating the extension VS Code will typically notify you when updates are available for installed extensions. Follow the prompts to update the Cody AI extension to the latest version. ## Authenticating Cody with VS Code forks Cody also works with Cursor, Gitpod, IDX, and other similar VS Code forks. To access VS Code forks like Cursor, select **Sign in with URL and access token** and generate an access token. Next, copy and paste into the allocated field, using `https://sourcegraph.com` as the URL. ## Supported LLM models Claude 3.5 Sonnet is the default LLM model for inline edits and prompts. If you've used a different or older LLM model for inline edits before, remember to manually change your model to Claude 3.5 Sonnet. Default model changes only affect new users. Users on Cody **Free** and **Pro** can choose from a list of [supported LLM models](/cody/capabilities/supported-models) for chat. ![LLM-models-for-cody-free](https://storage.googleapis.com/sourcegraph-assets/Docs/llm-dropdown-options-0225.jpg) Enterprise users get Claude 3.5 Sonnet as the default LLM models without extra cost. Moreover, Enterprise users can use Claude 3.5 models through Cody Gateway, Anthropic BYOK, Amazon Bedrock (limited availability), and GCP Vertex. For enterprise users on Amazon Bedrock: 3.5 Sonnet is unavailable in `us-west-2` but available in `us-east-1`. Check the current model availability on AWS and your customer's instance location before switching. Provisioned throughput via AWS is not supported for 3.5 Sonnet. You also get additional capabilities like BYOLLM (Bring Your Own LLM), supporting Single-Tenant and Self Hosted setups for flexible coding environments. Your site administrator determines the LLM, and cannot be changed within the editor. However, Cody Enterprise users when using Cody Gateway have the ability to [configure custom models](/cody/core-concepts/cody-gateway#configuring-custom-models) from Anthropic, OpenAI, and Google Gemini. Read more about all the supported LLM models [here](/cody/capabilities/supported-models) ## Experimental models Support for the following models is currently in the Experimental stage, and available for Cody Free and Pro plans. The following experimental model providers can be configured in Cody's extension settings JSON: - Google (requires [Google AI Studio API key](https://aistudio.google.com/app/apikey)) - Groq (requires [GroqCloud API key](https://console.groq.com/docs/api-keys)) - OpenAI & OpenAI-Compatible API (requires [OpenAI API key](https://platform.openai.com/account/api-keys)) - Ollama (remote) Once configured, and VS Code has been restarted, you can select the configured model from the dropdown both for chat and for edits. Example VS Code user settings JSON configuration: ```json { "cody.dev.models": [ { "provider": "google", "model": "gemini-2.0-flash-exp", "inputTokens": 1048576, "outputTokens": 8192, "apiKey": "", "options": { "temperature": 0.0 } }, { "provider": "groq", "model": "llama2-70b-4096", "inputTokens": 4000, "outputTokens": 4000, "apiKey": "", "options": { "temperature": 0.0 } }, { "provider": "openai", "model": "some-model-id", "inputTokens": 32000, "outputTokens": 4000, "apiKey": "", "options": { "temperature": 0.0 }, "apiEndpoint": "https://host.domain/path" }, { "provider": "ollama", "model": "some-model-id", "apiEndpoint": "https://host.domain/path" } ] } ``` ### Provider configuration options - `"provider"`: `"google"`, `"groq"`, `"ollama"` or `"openai"` - The LLM provider type. - `"model"`: `string` - The ID of the model, e.g. `"gemini-2.0-flash-exp"` - `"inputTokens"`: `number` - optional - The context window size of the model's input. Default: 7000. - `"outputTokens"`: `number` - optional - The context window size of the model's output. Default: 4000. - `"apiKey"`: `string` - optional - The API key for the endpoint. Required if the provider is `"google"`, `"groq"`, `"ollama"` or `"openai"`. - `"apiEndpoint"`: `string` - optional - The endpoint URL, if you don't want to use the provider’s default endpoint. - `"options"` : `object` - optional - Additional parameters like `temperature`, `topK`, `topP` based on provider documentation. ### Debugging experimental models To debug problems with the experimental models, use the VS Code output panel which can be opened using the following steps: - Open the Cody Sidebar - Next to "Settings and Support" click the "..." icon - Click "Open Output Channel" ## Add/remove account To add/remove an account you can do the following: 1. Open Cody by clicking the Cody icon on the left navbar 1. On the open sidebar select the Account icon 1. Select `Sign Out` to remove account or `Switch Account` to login to a different account
# Installing Cody in Visual Studio

Learn how to use Cody and its features with the Visual Studio editor.

Cody for Visual Studio is currently in the Experimental stage and currently supports chat and autocomplete. Cody extension for Visual Studio enhances your coding experience by providing intelligent and contextually aware answers to your questions. This guide will walk you through installing and setting Cody within your Visual Studio editor. ## Prerequisites - You have the latest version of [Visual Studio](https://visualstudio.microsoft.com/) installed - You have a Free or Pro account via Sourcegraph.com or a Sourcegraph Enterprise account ## Install the Visual Studio extension - Download the Cody extension for Visual Studio from the [GitHub repository](https://github.com/sourcegraph/cody-vs/releases) - Run the installer and follow the prompts to install the extension - Once installed, go to the **Extensions > Manage Extensions** tab in Visual Studio and search for Cody under the **Installed** list for confirmation ## Connect the extension to Sourcegraph Cody for Visual Studio is available for all Cody plans, including Cody Free, Pro, and Enterprise. After a successful installation, go to **Tools** from the main toolbar at the top and click the **Cody Chat** from the drop-down. This opens the dialog box to connect to your Sourcegraph instance. Cody Free or Pro users can sign in to their Sourcegraph.com accounts through GitHub, GitLab, or Google. Meanwhile, Sourcegraph Enterprise users should connect Cody via their Enterprise instance URL and the Access Token. Complete these steps, and you'll be ready to start using Cody in Visual Studio. ![install-cody-vscode](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-vs-setup-102024-2.png) ## Chat Cody in Visual Studio allows you to ask questions about your code and get contextually aware answers. The chat window is available in a unified interface next to your code. All your previous and existing chats are stored for later use and can be accessed via the **History** icon from the top menu. You can download them to share or use later in a `.json` file or delete them. The chat input field has a default `@-mention` [context chips](#context-retrieval). These are automatically populated with the names of the files you have open in your editor. There is also a drop-down for [LLM selection](#llm-selection) and a button to run pre-built [prompts and commands](#prompts). ![cody-vs-chat](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-visual-studio-1024.png) ## LLM selection Cody offers a variety of large language models (LLMs) to power your chat experience. Cody Free users can access the latest base models from Anthropic, OpenAI, Google. At the same time, Cody Pro and Enterprise users can access more extended models. You can read more about it in our [Supported LLM models docs](/cody/capabilities/supported-models). ## Selecting Context with @-mentions Cody's chat allows you to [add files and symbols as context](/cody/core-concepts/context) in your messages. - Type `@-file` and then a filename to include a file as a context - Type `@#` and then a symbol name to include the symbol's definition as context. Functions, methods, classes, types, etc., are all symbols ![cody-vs-context-retrieval](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-vs-mention-102024-2.png) ### Context retrieval When you start a new Cody chat, the chat input window opens with a default `@-mention` context chips for all the context it intends to use. This context is based on your current repository and current file (or a file selection if you have code highlighted). At any point in time, you can edit these context chips or remove them completely if you do not want to use these as context. Any chat without a context chip will instruct Cody to use no codebase context. However, you can always provide an alternate `@-mention` file or symbols to let Cody use it as a new context source. When you have both a repository and files @-mentioned, Cody will search the repository for context while prioritizing the mentioned files. ## Prompts Cody allows you create quick, ready-to-use [prompts](/cody/capabilities/commands) to automate key tasks in your workflow. Prompts are created and saved in the Prompt Library and can be accessed from the **Tools > Prompt Library** in the top navigation bar in your Sourcegraph instance. To help you get started, there are a few prompts that are available by default. These can assist you to: - Document code - Explain code - Detect code smells - Generate unit tests ![cody-vs-prompts](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-vs-prompts-102024-2.png) ## Autocomplete Cody for Visual Studio supports single and multi-line autocompletions. The autocomplete feature is available in the Cody extension starting in `v0.2.0` and requires a Visual Studios version of 17.8+ and above. It's enabled by default, with settings to turn it off. Advanced features like [auto-edit](/cody/capabilities/auto-edit) are not yet supported. To disable the autocomplete feature, you can do it from your Cody settings section.
# Install Cody for Neovim

Learn how to use Cody and its features with the Neovim editor.

Cody for Neovim is currently in the experimental stage and is no longer actively maintained, so support will be limited. The Cody extension for Neovim by Sourcegraph enhances your coding experience in your IDE by providing intelligent code suggestions, context-aware completions, and advanced code analysis. This guide will walk you through installing and setting up the Cody within your Neovim environment. ## Prerequisites - `nvim 0.9 or nvim nightly` version of Neovim installed - `Node.js >= 18.17.0 (LTS)` at runtime for [cody-agent.js](https://github.com/sourcegraph/cody) - You have enabled an instance for [Cody from your Sourcegraph.com](/cody/clients/cody-with-sourcegraph) account ## Installation `sg.nvim` is a plugin that uses Sourcegraph's code intelligence features directly within the Neovim text editor. You can install the plugin using different Neovim plugin managers like: ### `lazy.nvim` ```lua return { { "sourcegraph/sg.nvim", dependencies = { "nvim-lua/plenary.nvim" }, -- If you have a recent version of lazy.nvim, you don't need to add this! build = "nvim -l build/init.lua", }, } ``` ### `packer.nvim` ```lua -- Packer.nvim, also make sure to install nvim-lua/plenary.nvim use { 'sourcegraph/sg.nvim', run = 'nvim -l build/init.lua' } ``` ### `vim-plug` ```lua -- Using vim-plug Plug 'sourcegraph/sg.nvim', { 'do': 'nvim -l build/init.lua' } ``` Once you have installed the plugin, run `:checkhealth sg` to verify a successful installation. Next, you are prompted to log in as a free user by connecting to your Sourcegraph.com account or using the enterprise instance. ## Setting up with Sourcegraph instance To connect `sg.nvim` with Sourcegraph, you need to follow these steps: - Log in on your Sourcegraph instance - Go to **Settings > Access tokens** from the top right corner - Create your access token, and then run `:SourcegraphLogin` in your neovim editor after installation - Type in the link to your Sourcegraph instance (for example, https://sourcegraph.com) - Next, paste your generated access token An alternative way to this is to use the environment variables specified for [`src-cli`](https://github.com/sourcegraph/src-cli#log-into-your-sourcegraph-instance). At any point, you can run `:checkhealth sg` to ensure you're logged in and connected to your Sourcegraph instance. ## Features The `sg.nvim` plugin supports a wide range of features that helps you integrate and use Cody and Sourcegraph Search directly within your Neovim environment. ### Cody The `sg.nvim` extension supports the following features for Cody: | **Feature** | **Description** | | ------------------- | --------------------------------------------------- | | **Chat** | Chat interface and associated commands | | **Autocompletions** | Support both prompted and suggested autocompletions | ### Search The `sg.nvim` extension supports the following features for Sourcegraph Search: | **Feature** | **Description** | | ---------------------------- | --------------------------------------------------------------------------------------------- | | **Read files** | Directly from sourcegraph links: `:edit ` | | | Automatically adds protocols for `https://sourcegraph.com/*` links | | | Directly from buffer names `:edit sg://github.com/tjdevries/sam.py/-/src/sam.py` | | **Read non-files** | Repository roots, folders (both expanded and non-expanded), open file from folder | | **Built-in LSP client** | Connects to Sourcegraph via `Goto Definition` and `Goto References` (less than 20 references) | | **Basic search** | Keyword and regexp support, `type:symbol` support, and repo support | | **Advanced search features** | Autocompletions and memory of last searches | ## Commands The `sg.nvim` extension also supports pre-built reusable prompts for Cody called "Commands" that help you quickly get started with common programming tasks like: - `:CodyAsk`: Ask a question about the current selection - `:CodyChat {title}`: Starts a new Cody chat, with an optional `{title}` - `:CodyRestart`: Restarts Cody and Sourcegraph - `:CodyTask {task_description}`: Instructs Cody to perform a task on a selected text - `:CodyTaskAccept`: Accepts the current `CodyTask` - `:CodyTaskNext`: Cycles to the next `CodyTask` - `:CodyTaskPrev`: Cycles to the previous `CodyTask` - `:CodyTaskView`: Opens the last active `CodyTask` - `:CodyToggle`: Toggle to the current Cody Chat window
# Installing Cody for JetBrains

Learn how to use Cody and its features with JetBrains editors.

The Cody plugin by Sourcegraph enhances your coding experience in your IDE by providing intelligent code suggestions, context-aware completions, and advanced code analysis. This guide will walk you through the steps to install and set up Cody within your JetBrains environment. ## Prerequisites - You have the latest version of JetBrains IDEs installed - You have a Free or Pro account via Sourcegraph.com or a Sourcegraph Enterprise account - Cody is compatible with the following JetBrains IDEs: - [Android Studio](https://developer.android.com/studio) - [AppCode](https://www.jetbrains.com/objc/) - [CLion](https://www.jetbrains.com/clion/) - [DataGrip](https://www.jetbrains.com/datagrip/) - [GoLand](https://www.jetbrains.com/go/) - [IntelliJ IDEA](https://www.jetbrains.com/idea/) (Community and Ultimate editions) - [PhpStorm](https://www.jetbrains.com/phpstorm/) - [PyCharm](https://www.jetbrains.com/pycharm/) (Community and Professional editions) - [Rider](https://www.jetbrains.com/rider/) - [RubyMine](https://www.jetbrains.com/ruby/) - [WebStorm](https://www.jetbrains.com/webstorm/) ## Install the JetBrains Cody plugin Follow these steps to install the Cody plugin: - Open a supported JetBrains editor on your local machine - Open **Settings** (macOS: `⌘+,` Windows: `Ctrl+Alt+S`) and select **Plugins** - Search for **Cody: AI Coding Assistant with Autocomplete & Chat** in the marketplace and click **Install** ![cody-for-intellij](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-for-intellij-062024.png) Alternatively, you can also [download and install the plugin from the JetBrains marketplace](https://plugins.jetbrains.com/plugin/9682-sourcegraph) directly. ## Connect the plugin to Sourcegraph After a successful installation, the Cody icon appears in the Tool Windows Bar. ### Cody Free or Cody Pro Users Cody Free and Pro users can sign in to their Sourcegraph.com accounts using SSO through GitHub, GitLab, or Google. ![cody-for-intellij-login](https://storage.googleapis.com/sourcegraph-assets/Docs/sign-in-cody-jb-2025.jpg) ### Sourcegraph Enterprise Cody Users Sourcegraph Enterprise users should connect Cody to their Enterprise instance by clicking **Sign in with an Enterprise Instance**. To connect the plugin with your Enterprise instance, - Click **Sign in with an Enterprise Instance** - Enter the URL of your Enterprise instance. If you are unsure, please get in touch with your administrator - Select **Authorize in Browser**. You'll be directed to an authorization page on your instance in the browser Alternatively, you can access advanced authorization settings by clicking **Show Advanced**. You can manually enter a token generated from your User Settings in your Sourcegraph Enterprise instance or add optional custom request headers. ## Verifying the installation Once connected, click the Cody icon from the sidebar again. The Cody plugin will open in a configurable side panel. Let's create an autocomplete suggestion to verify that the Cody plugin has been installed and works as expected. Cody provides intelligent code suggestions and context-aware autocompletion for numerous programming languages, such as JavaScript, Python, TypeScript, Go, etc. - Create a new file in your JetBrains IDE, for example, `code.js` - Next, type the following algorithm function to sort an array of numbers ```js function bubbleSort(array){ } ``` - As you start typing, Cody will automatically provide suggestions and context-aware completions based on your coding patterns and the code context - These autocomplete suggestions appear as grayed text. To accept the suggestion, press the `Tab` key ## Chat Cody chat in JetBrains is available in a unified interface that opens right next to your code. Once connected to Sourcegraph, a new chat input field opens with the default `@-mention` [context chips](#context-retrieval). All your previous and existing chats are stored for later use and can be accessed via the **History** icon from the top menu. You can download them to share or use later in a `.json` file or delete them. ### Chat interface The chat interface is designed intuitively. Your very first chat input lives at the top of the panel, and the first message in any chat log will stay pinned to the top of the chat. After your first message, the chat input window moves to the bottom of the sidebar. Since your first message to Cody anchors the conversation, you can return to the top chat box anytime, edit your prompt, or rerun it using a different LLM model. ![chat-interface](https://storage.googleapis.com/sourcegraph-assets/Docs/jb-chat-interface-2025.png) Users must be on JetBrains v2023.2 and Cody plugin v7.0.0 or above to get the new and improved chat UI. ### Chat History A chat history icon at the top of your chat input window allows you to navigate between chats (and search chats) without opening the Cody sidebar. ### Changing LLM model for chat You need to be a Cody Free or Pro user to have multi-model selection capability. You can view which LLMs you can access on our [supported LLMs page](/cody/capabilities/supported-models). Enterprise users with the new [model configuration](/cody/clients/model-configuration) can use the LLM selection dropdown to choose a chat model. For Chat: - Open chat or toggle between editor and chat - Click on the model selector (which by default indicates Claude 3.5 Sonnet) - See the selection of models and click the model you desire. This model will now be the default model for any new chats For Edit: - On any file, select some code and right-click - Select **Cody > Edit Code** (optionally, you can do this with `Opt+K`/`Alt+K`) - Select the default model available - See the selection of models and click the model you desire. This model will now be the default model for any new edits ### Selecting Context with @-mentions Cody's chat allows you to add files as context in your messages. - Type `@-file` and then a filename to include a file as a context. The `@-file` also supports line numbers to query the context of large files. You can add ranges of large files to your context by @-mentioning a large file and appending a number range to the filename, for example, `@filepath/filename:1-10`. When you `@-mention` files to add to Cody’s context window, the file lookup takes `files.exclude`, `search.exclude`, and `.gitgnore` files into account. This makes the file search faster as a result up to 100ms. Moreover, when you `@-mention` files, Cody will track the number of characters in those files against the context window limit of the selected chat model. As you `@-mention` multiple files, Cody will calculate how many tokens of the context window remain. When the remaining context window size becomes too small, you'll receive **File too large** errors when attempting to `@-mention` additional files. You can read more about context limits when selecting context [here](/cody/core-concepts/token-limits). ### Context retrieval When you start a new Cody chat, the input window opens with default `@-mention` context chips for all the context it intends to use. This context is based on your current repository and current file (or a file selection if you have code highlighted). ![jb-context-retrieval](https://storage.googleapis.com/sourcegraph-assets/Docs/jb-context-retrieval-2025.png) At any point in time, you can edit these context chips, add additional context chips, or remove them completely if you do not want to use these as context. Any chat without a context chip will instruct Cody to use no codebase context. However, you can always provide an alternate `@-mention` file to let Cody use it as a new context source. When you have both a repository and files @-mentioned, Cody will search the repository for context while prioritizing the mentioned files. ### Rerun prompts with different context If Cody's answer isn't helpful, you can try asking again with a different context: - **Public knowledge only**: Cody will not use your code files as context; it’ll only use knowledge trained into the base model. - **Current file only**: Rerun the prompt using just the current file as context. - **Add context**: Provides @-mention context options to improve the response by explicitly including files, remote repositories, or web pages (URL). ![jb-rerun-context](https://storage.googleapis.com/sourcegraph-assets/Docs/jb-rerun-context-2025.png) ## Context fetching mechanism JetBrains users on the Free or Pro plan use [local context](/cody/core-concepts/context#context-sources). Enterprise users can leverage the full power of the Sourcegraph search engine as Cody's primary context provider. Read more about [Context fetching mechanisms](/cody/core-concepts/context/#context-fetching-mechanism) in detail. ## Context sources You can @-mention files and web pages in Cody. Cody Enterprise also supports @-mentioning repositories to search for context in a broader scope. Cody Free and Pro offer single-repo context, and Cody Enterprise supports multi-repo context. ### Cody Context Filters Context Filters is available for all Cody Enterprise users running Cody JetBrains plugin version `>=6.0.0`. Admins on the Sourcegraph Enterprise instance can use Cody Context Filters to determine which repositories Cody can use as the context in its requests to third-party LLMs. Inside your site configuration, you can define a set of `include` and `exclude` rules that will be used to filter the list of repositories Cody can access. For repos mentioned in the `exclude` field, Cody prompts are disabled, and you cannot use them for context fetching. If you try running any of these, you'll be prompted with an error message. However, Cody chat will still work, and you can use it to ask questions. [Read more about Cody Context Filters here →](/cody/capabilities/ignore-context) ## Autocomplete Cody provides multi-line autocomplete as you type. Autocomplete suggestions appear as inlay suggestions and are enabled by default in your JetBrains IDE. This setting lists the programming languages supported and enabled by default. To manually configure the Autocomplete feature, - Go to the **Cody Settings...** from the Cody icon in the sidebar - Next, click the **Sourcegraph & Cody** dropdown and select **Cody** - The **Autocomplete** settings will appear with the list of **Enabled Languages** Autocomplete suggestions use the same color as inline parameter hints according to your configured editor theme. However, you can optionally enable the **Custom color for completions** checkbox to customize the color of your choice. In addition, you can use the following keyboard shortcuts to interact with Cody's autocomplete suggestions: - `Tab` to accept a suggestion - `Alt + [` (Windows) or `Opt + [` (macOS) to cycle suggestions - `Alt + \` (Windows) or `Opt + \` (macOS) to manually trigger autocomplete if no suggestions have been returned ## Prompts Cody allows you create quick, ready-to-use [prompts](/cody/capabilities/commands) to automate key tasks in your workflow. Prompts are created and saved in the Prompt Library and can be accessed from the **Tools > Prompt Library** in the top navigation bar in your Sourcegraph instance. To help you get started, there are a few prompts that are available by default. These can assist you to: - Document code - Explain code - Detect code smells - Generate unit tests ![jb-default-prompts](https://storage.googleapis.com/sourcegraph-assets/Docs/jb-default-prompts-2025.png) ### Inline code edits You can directly perform inline edits on your code without opening the chat window. - Select the code you want to edit - Then right-click and select **Cody > Edit Code** - Alternatively, you can press Opt+K (macOS) or Alt+K (Windows) to open the inline editor Here, you can describe the change you want to make and select the LLM model you want to use. Once you enter your prompt, Cody will perform inline edits that you can **Accept**, **Undo**, or **Show diff** for the change. Click **Edit & Retry** to iterate your prompt and get alternate suggestions. ![jb-inline-code-edit](https://storage.googleapis.com/sourcegraph-assets/Docs/jb-inline-edits-2025.png) ### Inline code fix Cody with JetBrains can also propose fixes and updates to errors in your code. The inline **Ask Cody to Fix** functionality alerts a user when it notices an issue. It provides a workflow to suggest a fix, review the suggestion, and accept the change, all within the editor. All you need to do is select and highlight the code line with the error and click the lightbulb icon. Then select **Ask Cody to Fix**. You can then view the diff and accept or undo the suggested change. ## Updating the plugin JetBrains IDEs will typically notify you when updates are available for installed plugins. Follow the prompts to update the Cody AI plugin to the latest version. ## Change the update channel for stable or nightly releases Our nightly release channel gets updated much more frequently, which might help verify bug fixes that will be included in the next stable release. To update your update channel, you can do the following: 1. Open your JetBrains IDE settings by selecting **IDE Name | Settings** on macOS or **File | Settings** on Windows and Linux from the main menu. 1. Get to the Cody Settings by navigating to `Tools -> Sourcegraph & Cody` 1. Under the update channel, select `Stable` or `Nightly` ## Supported LLM models Cody Free and Pro users can choose from a list of supported LLM models for chat. ![llm-selection-cody](https://storage.googleapis.com/sourcegraph-assets/Docs/jb-llm-select-2025.png) Enterprise users who have [model configuration](/Cody/clients/model-configuration#model-configuration) configured can also select from the available models for their instance. On instances with the ["completions" configuration](/Cody/clients/model-configuration#completions-configuration), a site admin determines the LLM, which cannot be changed within the editor. Read and learn more about the [supported LLMs](/cody/capabilities/supported-models) and [token limits](/cody/core-concepts/token-limits) on Cody Free, Pro and Enterprise. ## Add/remove account To add or remove an account, you can do the following: 1. Open Cody by clicking the Cody icon on the toolbar 1. On the open sidebar, select the Account icon 1. Select `Sign Out` to remove an account Alternatively, you can also manage multiple accounts in Cody Settings: 1. Open your IDE settings by selecting **IDE | Settings** on macOS or **File | Settings** on Windows and Linux from the main menu. 1. Go to the Cody Settings by navigating to `Tools -> Sourcegraph & Cody` 1. Under authentication, see the accounts that are currently logged in 1. To remove, select your account and hit `-`. To add click `+` and choose the appropriate login method ## Find Cody features You can find and discover all Cody features and actions using the **Search Everywhere** option in JetBrains IDEs. Press `Shift` twice to open the `Search Everywhere` window. Then, type in the `Cody:` prefix to get a list of all supported Cody actions. ![search-everywhere](https://storage.googleapis.com/sourcegraph-assets/Docs/search-everywhere-cody.png)
# Installing Cody in Eclipse

Learn how to use Cody and its features with the Eclipse editor.

Cody for Eclipse is currently in the Experimental stage and supports only chat. It is compatible with Eclipse version 2024-03 (4.31.0) and runs on Windows 11. The support is also limited feel free to contact us for any questions or feedback. Cody extension for Eclipse enhances your coding experience by providing intelligent and contextually aware answers to your questions. This guide will walk you through installing and setting Cody within your Eclipse editor. ## Prerequisites - You have the correct version of [Eclipse](https://www.eclipse.org/downloads/packages/release/2024-03/r) IDE installed - You have a Free or Pro account via Sourcegraph.com or a Sourcegraph Enterprise account ## Install the Eclipse extension - Inside Eclipse, go to **Help > Install New Software** - Next, add the site URL `https://sourcegraph.github.io/eclipse` - After adding this URL, you should see the **Cody** category in the list of available plugins ![add-eclispe-extension](https://storage.googleapis.com/sourcegraph-assets/Docs/add-eclipse-url-1124.png) - Click **Next** and follow the installation instructions - After you have completed the installation and restarted Eclipse, you should see the **Cody** view in the **Window > Show View > Other** menu ![eclipse-cody-view](https://storage.googleapis.com/sourcegraph-assets/Docs/eclipse-cody-view-1124.png) ## Connect the extension to Sourcegraph Cody for Eclipse is available for all Cody plans, including Cody Free, Pro, and Enterprise. After a successful installation, open the **Cody** view. You should see a button to sign into your Sourcegraph account. ![eclipse-cody-sign-in](https://storage.googleapis.com/sourcegraph-assets/Docs/eclispe-signin-sourcegraph-1124-2.png) Cody Free or Pro users can sign in to their Sourcegraph.com accounts. Meanwhile, Sourcegraph Enterprise users should connect Cody via their Enterprise instance URL and the Access Token. Complete these steps, and you'll be ready to use Cody chat in Eclipse. ## Chat Cody in Eclipse allows you to ask questions about your code and get contextually aware answers. The chat window is available in a unified interface next to your code. All your previous and existing chats are stored for later use and can be accessed via the **History** icon from the top menu. You can download them to share or use later in a `.json` file or delete them. The chat input field has a default `@-mention` [context chips](#context-retrieval). These are automatically populated with the names of the files you have open in your editor. There is also a drop-down for [LLM selection](#llm-selection) and a button to run pre-built [prompts](#prompts). ![cody-eclipse-chat](https://storage.googleapis.com/sourcegraph-assets/Docs/eclipse-cody-chat-1124.png) ## LLM selection Cody offers a variety of large language models (LLMs) to power your chat experience. Cody Free users can access the latest base models from Anthropic, OpenAI, Google. At the same time, Cody Pro and Enterprise users can access more extended models. You can read more about it in our [Supported LLM models docs](/cody/capabilities/supported-models). ## Selecting Context with @-mentions Cody's chat allows you to [add files and symbols as context](/cody/core-concepts/context) in your messages. - Type `@-file` and then a filename to include a file as a context - Type `@#` and then a symbol name to include the symbol's definition as context. Functions, methods, classes, types, etc., are all symbols ### Context retrieval When you start a new Cody chat, the chat input window opens with a default `@-mention` context chips for all the context it intends to use. This context is based on your current repository and current file (or a file selection if you have code highlighted). At any point in time, you can edit these context chips or remove them completely if you do not want to use these as context. Any chat without a context chip will instruct Cody to use no codebase context. However, you can always provide an alternate `@-mention` file or symbols to let Cody use it as a new context source. When you have both a repository and files @-mentioned, Cody will search the repository for context while prioritizing the mentioned files. ## Prompts Cody offers a variety of [pre-built prompts](/cody/capabilities/commands) to help you get the most out of your chat experience. You can access these prompts from the chat input field. ## Feedback While Cody for Eclipse is currently in the experimental stage, we are open to feedback [in our forum](https://community.sourcegraph.com/c/cody/eclipse/13).
# Install Cody CLI

Learn how to install the cody command-line tool and using the cody chat subcommand.

Cody CLI support is in the Experimental stage for Enterprise accounts. Cody CLI is the same technology that powers the Cody IDE plugins but available from the command-line. Use Cody CLI for ad-hoc exploration in your terminal or as part of scripts to automate your workflows. ## Prerequisites - You have Node.js `v20` or newer installed - You have `npm`, `yarn`, `pnpm`, or an equivalent package manager installed ## Install CLI from npm Run the following command to install the Cody CLI: ```shell npm install -g @sourcegraph/cody ``` ```shell yarn global add @sourcegraph/cody ``` ```shell pnpm install -g @sourcegraph/cody ``` Confirm that the installation was successful by running the following command: ```shell cody help ``` ## Authenticate with cody auth login To start using the Cody CLI, you need to authenticate it with your Sourcegraph account. When using `cody auth login`, the access token is stored in the secure storage of your operating system or equivalent security tool. If you prefer not to let Cody store your access token, authenticate with [environment variables](#authenticate-with-only-environment-variables) instead. First, make sure you have installed the necessary tools to use `cody auth login`. The pre-installed `security` tool is used. No additional installation is required. The [CredentialManager](https://www.powershellgallery.com/packages/CredentialManager/2.0) PowerShell module is required. Open a PowerShell terminal with administrator privileges and run the following command: ```powershell Install-Module -Name CredentialManager ``` The [`secret-tool`](https://manpages.ubuntu.com/manpages/focal/man1/secret-tool.1.html#:~:text=secret%2Dtool%20is%20a%20command,of%20attribute%20keys%20and%20values) command-line tool is used. Run the following commands to install it: ```shell sudo apt install libsecret-tools sudo apt install gnome-keyring ``` Run the following command to authenticate the Cody CLI: ```shell cody auth login --web ``` This will open a browser window where you can authenticate with your Sourcegraph account. Close the browser tab after authentication is complete. - Cody Enterprise accounts can sign into their Sourcegraph Enterprise account and create an access token under `Account > Settings > Access Tokens`. - ```shell export SRC_ENDPOINT=ENDPOINT export SRC_ACCESS_TOKEN=ACCESS_TOKEN cody auth login # stores the access token securely ``` You don't need to export `SRC_ENDPOINT` or `SRC_ACCESS_TOKEN` after running `cody auth login`. Subsequent command invocation will use the stored access token. Confirm that the authentication was successful by running the following command: ```shell cody auth whoami # ✔ Authenticated as USERNAME on ENDPOINT ``` ## Authenticate with only environment variables **Skip this step if you have already authenticated with the `cody auth login` command.** If you prefer not to let Cody CLI store your access token, you can also pass the endpoint URL and access token through the environment variables `SRC_ENDPOINT` and `SRC_ACCESS_TOKEN`. ```shell export SRC_ENDPOINT=ENDPOINT export SRC_ACCESS_TOKEN=ACCESS_TOKEN ``` ```powershell $env:SRC_ENDPOINT = "ENDPOINT" $env:SRC_ACCESS_TOKEN = "ACCESS_TOKEN" ``` It's recommended to store these access tokens in a secure location. For example, you can store them with a password manager like [1Password](https://1password.com/) or [Bitwarden](https://bitwarden.com/). It is not recommended to export these variables in your shell startup script because it will expose your access token to all commands you run from the terminal. Instead, consider sourcing these environment variables on-demand when you need to authenticate with the Cody CLI. ## Sign out with cody auth logout To sign out of the Cody CLI, run the following command: ```shell cody auth logout ``` Running this command wil remove the access token from the secure storage of your operating system. Run `cody auth whoami` to confirm that the Cody CLI is no longer authenticated. ## Chat with basic message Once you've authenticated the Cody CLI, you can start using it to chat with Cody. To start a new chat, run the following command: ```shell cody chat -m 'Explain React hooks' ``` The following commands are equivalent to the above: ```shell # use --message instead of -m cody chat --message 'Explain React hooks' # space separated arguments cody chat Explain React hooks ``` ## Chat with --context-file to add context from local files Use `--context-file` to provide context from local files ```shell cody chat --context-file src/controller.ts -m 'Are there code smells in this file?' ``` ## Chat with --context-repo to add context from remote repositories This feature is only supported on Cody Enterprise. Use `--context-repo` to provide context from remote repositories. ```shell cody chat --context-repo github.com/sourcegraph/cody -m 'What is the agent?' ``` ## Chat with --stdin to read message from standard input Use `--stdin` to provide context from standard input ```shell echo 'Explain React hooks' | cody chat --stdin ``` Combine `--stdin` with `--message` to send a concatenated message. The `--message` string appears at the top of the prompt and the `--stdin` text appears at the bottom. ```shell git diff | cody chat --stdin -m 'Write a commit message for this diff' ``` Use the `-` trailing argument as an alternative to `--stdin` to read the diff from standard input. ```shell git diff | cody chat -m 'Write a commit message for this diff' - ```
# Cody Clients

There are multiple ways to use Cody: you can install its extension in your favorite IDEs, access it via the Sourcegraph web app, or use it through the Cody CLI.

# Feature Parity Reference for Sourcegraph Clients

This document compares features and capabilities across different clients and platforms like VS Code, JetBrains, and Sourcegraph.com (Web UI).

## Chat | **Feature** | **VS Code** | **JetBrains** | **Visual Studio** | **Web** | **CLI** | | ---------------------------------------- | ----------- | ------------- | ----------------- | -------------------- | ------- | | Chat | ✅ | ✅ | ✅ | ✅ | ✅ | | Chat history | ✅ | ✅ | ✅ | ✅ | ❌ | | Clear chat history | ✅ | ✅ | ✅ | ✅ | ❌ | | Edit sent messages | ✅ | ✅ | ✅ | ✅ | ❌ | | SmartApply/Execute | ✅ | ❌ | ❌ | ❌ | ❌ | | Show context files | ✅ | ✅ | ✅ | ✅ | ❌ | | @-file | ✅ | ✅ | ✅ | ✅ | ❌ | | @-symbol | ✅ | ❌ | ✅ | ✅ | ❌ | | LLM Selection | ✅ | ✅ | ✅ | ✅ | ❌ | | Agentic Context Fetching | ✅ | ✅ | ✅ | ✅ | ✅ | | **Context Selection** | | | | | | | Single-repo context | ✅ | ✅ | ✅ | ✅ | ❌ | | Multi-repo context | ❌ | ❌ | ❌ | ✅ (public code only) | ❌ | | Local context | ✅ | ✅ | ✅ | ❌ | ✅ | | OpenCtx context providers (experimental) | ✅ | ❌ | ❌ | ❌ | ❌ | | **Prompts** | | | | | | | Access to prompts and Prompt library | ✅ | ✅ | ✅ | ✅ | ❌ | | Promoted Prompts | ✅ | ❌ | ❌ | ✅ | ❌ | ## Code Autocomplete and Auto-edit | **Feature** | **VS Code** | **JetBrains** | **Visual Studio** | | ----------------------------------------------------- | ----------- | ------------- | ----------------- | | Single and multi-line autocompletion | ✅ | ✅ | ✅ | | Cycle through multiple completion suggestions | ✅ | ✅ | ✅ | | Accept suggestions word-by-word | ✅ | ❌ | ❌ | | Auto-edit suggestions via cursor movements and typing | ✅ | ✅ | ❌ | Few exceptions that apply to Cody Pro and Cody Enterprise users: - Multi-repo context is not supported in VS Code, JetBrains, or the Web UI for Cody Pro - Admin LLM selection is suported on VS Code, JetBrains, Visual Studio, and Web both for chat and code autocomplete - Multi-repo context is supported on VS Code, JetBrains, Visual Studio, and Web - [Guardrails](/cody/clients/enable-cody-enterprise#guardrails) are supported on VS Code, JetBrains, and Web - [Repo-based Cody Context Filters](/cody/capabilities/ignore-context#cody-context-filters) are supported on all Cody clients. - `@-mention` directories are supported on VS Code, JetBrains, Visual Studio, and Web
# Cody for Enterprise

Cody enhances your coding experience by providing intelligent code suggestions, context-aware completions, and advanced code analysis. These docs will help you use Cody on your Sourcegraph Enterprise instance.

## Setting up Cody Enterprise You can set up Cody for your Enterprise instance by two methods: 1. Sourcegraph Cloud 2. Self-hosted Sourcegraph ## Cody on Sourcegraph Cloud With [Sourcegraph Cloud](/cloud/), you get Cody as a managed service, and you **do not** need to enable Cody as is required for self-hosted setup. However, by contacting your account manager, Cody can still be enabled or disabled on-demand on your Sourcegraph instance. ## Self-hosted Sourcegraph Enterprise ### Prerequisites - You have Sourcegraph version `5.1.0` or more - A Sourcegraph Enterprise subscription with [Cody Gateway](/cody/core-concepts/cody-gateway) or an account with a third-party LLM provider ### Enable Cody on your Sourcegraph instance Site admins can only enable Cody on the Sourcegraph instance. To do so, - First, configure your desired LLM provider either by [using Sourcegraph Cody Gateway](/cody/core-concepts/cody-gateway) or by directly using a third-party LLM provider - Next, go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, "completions": { "provider": "sourcegraph" } } ``` - Cody is enabled on your self-hosted Sourcegraph enterprise instance ## Disable Cody To turn Cody off: - Go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": false } ``` ## Enable Cody only for some users How to enable Cody only for _some_ users depends on what version of Sourcegraph you are running. ### Sourcegraph v5.3+ In Sourcegraph v5.3+, access to Cody is managed via user roles. By default, all users have access. First, ensure Cody is enabled in your site configuration. Go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, // Make sure cody.restrictUsersFeatureFlag is not in your configuration! If it is, remove it. } ``` Ensure `cody.restrictUsersFeatureFlag` is **not** in your site configuration. If it is, remove it or else the old feature-flag approach from Sourcegraph 5.2 and earlier will be used. Next, go to **Site admin > Users & Auth > Roles** (`/site-admin/roles`) on your instance. On that page, you can: - Control whether users **by default** have access to Cody (expand `User [System]` and toggle **Cody** > **Access** as desired) - Control whether groups of users have access to Cody (`+Create role` and enable the **Cody** > **Access** toggle as desired) ### Sourcegraph v5.2 and earlier In Sourcegraph v5.2 and earlier, you should use the feature flag `cody` to turn Cody on selectively for some users. To do so: - Go to **Site admin > Site configuration** (`/site-admin/configuration`) on your instance and set: ```json { // [...] "cody.enabled": true, "cody.restrictUsersFeatureFlag": true } ``` - Next, go to **Site admin > Feature flags** (`/site-admin/feature-flags`) - Add a feature flag called `cody` - Select the `boolean` type and set it to `false` - Once added, click on the feature flag and use **add overrides** to pick users that will have access to Cody ![add-overrides](https://user-images.githubusercontent.com/25070988/235454594-9f1a6b27-6882-44d9-be32-258d6c244880.png) ## Configure Cody for LLM providers Cody supports several LLM providers and models. You can access these models via the Cody Gateway, directly using your own model provider account or infrastructure. There are two ways of configuring Cody for LLM providers:
# Cody for Web

Learn how to use Cody in the web interface with your Sourcegraph.com instance.

In addition to the Cody extensions for [VS Code](/cody/clients/install-vscode), [JetBrains](/cody/clients/install-jetbrains), and [Visual Studio](/cody/clients/install-visual-studio ) IDEs, Cody is also available in the Sourcegraph web app. Community users can use Cody for free by logging into their accounts on Sourcegraph.com, and enterprise users can use Cody within their Sourcegraph instance. ## Initial setup Create a [Sourcegraph.com account](https://sourcegraph.com/sign-up) by logging in through codehosts like GitHub and GitLab or via traditional Google sign-in. This takes you to Sourcegraph’s web interface. From here, there are two ways to access the Cody chat: 1. Run any search query via **Code Search** and click the **Cody** button on the left to open the chat window 2. Directly click the **Chat** tab from the top header to open the chat interface ![cody-web](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-web-2025.png) Enterprise users can also log in to their Sourcegraph.com Enterprise instance and use Cody in the web interface. ## Chat interface The Cody chat interface for the web is similar to the one you get with the IDE extensions. However, the chat experience is slightly different depending on whether you use Cody with your search query results or directly from the top header. The chat interface with your Code Search queries opens parallel to your query search results, similar to the chat window in the IDE extensions. However, when you click **Cody** from the top header in your Sourcegraph.com instance, the chat interface opens on a new page. The new and improved chat UI for Cody for the web is currently available to users on Sourcegraph versions >=5.5. To use this new chat interface, you should update your Sourcegraph instance to the latest version. ## Chat with Cody on the web interface The feature set for the Cody chat is the same as the IDE extensions. Your previous chats can be viewed from the **History** tab. Claude 3.5 Sonnet (New) is selected as the default chat model. You can change this LLM model based on your use case to optimize speed, accuracy, or cost. Enterprise users with the new [model configuration](/cody/clients/model-configuration) can use the LLM selection dropdown to choose a chat model. You can read about these supported LLM models [here](/cody/capabilities/supported-models#chat-and-commands). To help you automate your key tasks in your development workflow, you get **[Prompts](/cody/capabilities/commands)**. If you are a part of an organization on Sourcegraph.com or a self-hosted Sourcegraph instance, you can view these pre-built Prompts created by your teammates. On the contrary, you can create your Prompts via the **Prompt Library** from your Sourcegraph instance. ## Context selection If you use Cody with your search results, the chat input will, by default, have the context of your searched codebase. This context is based on your current repository and file. ![cody-web-with-search](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-web-search-results-2025.png) You can add or delete any new or existing context by `@-mentioning` files, symbols, directories, repositories, and web URLs. Enterprise users can `@-mention` remote directories as well. When you have both a repository and files as context, Cody will search the repository for context while prioritizing the mentioned files. If you use Cody directly from the top header, the chat input will have no pre-filled context chips by default. You can chat without context or add or delete any new or existing context by `@-mentioning` context chips. ![cody-web-no-search](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-web-top-header-2025.png) ### Rerun prompts with different context If Cody's answer isn't helpful, you can try asking again with a different context: - **Public knowledge only**: Cody will not use your code files as context; it’ll only use knowledge trained into the base model - **Add context**: Provides @-mention context options to improve the response by explicitly including files, symbols, and remote repositories ![re-run-prompts](https://storage.googleapis.com/sourcegraph-assets/Docs/rerun-prompt-web-2025.png) For a detailed breakdown of Cody's capabilities across different clients [read these feature parity docs](/cody/clients/feature-reference). ## Prompts Cody allows you create quick, ready-to-use [prompts](/cody/capabilities/commands) to automate key tasks in your workflow. Prompts are created and saved in the Prompt Library and can be accessed from the **Tools > Prompt Library** in the top navigation bar in your Sourcegraph instance. ![cody-web-prompts](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-web-prompts-2025.png)
# Supported LLMs ## Chat and Prompts Cody supports a variety of cutting-edge large language models for use in chat and prompts, allowing you to select the best model for your use case. Newer versions of Sourcegraph Enterprise, starting from v5.6, it will be even easier to add support for new models and providers, see [Model Configuration](/cody/enterprise/model-configuration) for more information. | **Provider** | **Model** | **Free** | **Pro** | **Enterprise** | | | | | | :------------ | :-------------------------------------------------------------------------------------------------------------------------------------------- | :----------- | :----------- | :------------- | --- | --- | --- | --- | | OpenAI | [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo#:~:text=TRAINING%20DATA-,gpt%2D4%2D0125%2Dpreview,-New%20GPT%2D4) | - | ✅ | ✅ | | | | | | OpenAI | [GPT-4o](https://platform.openai.com/docs/models#gpt-4o) | - | ✅ | ✅ | | | | | | OpenAI | [GPT-4o-mini](https://platform.openai.com/docs/models#gpt-4o-mini) | ✅ | ✅ | ✅ | | | | | | OpenAI | [o3-mini-medium](https://openai.com/index/openai-o3-mini/) (experimental) | ✅ | ✅ | ✅ | | | | | | OpenAI | [o3-mini-high](https://openai.com/index/openai-o3-mini/) (experimental) | - | - | ✅ | | | | | | OpenAI | [o3](https://platform.openai.com/docs/models#o3) | - | ✅ | ✅ | | | | | | OpenAI | [o4-mini](https://platform.openai.com/docs/models/o4-mini) | ✅ | ✅ | ✅ | | | | | | OpenAI | [GPT-4.1](https://platform.openai.com/docs/models/gpt-4.1) | - | ✅ | ✅ | | | | | | OpenAI | [GPT-4.1-mini](https://platform.openai.com/docs/models/gpt-4o-mini) | ✅ | ✅ | ✅ | | | | | | OpenAI | [GPT-4.1-nano](https://platform.openai.com/docs/models/gpt-4.1-nano) | ✅ | ✅ | ✅ | | | | | | Anthropic | [Claude 3.5 Haiku](https://docs.anthropic.com/claude/docs/models-overview#model-comparison) | ✅ | ✅ | ✅ | | | | | | Anthropic | [Claude 3.5 Sonnet](https://docs.anthropic.com/claude/docs/models-overview#model-comparison) | ✅ | ✅ | ✅ | | | | | | Anthropic | [Claude 3.7 Sonnet](https://docs.anthropic.com/claude/docs/models-overview#model-comparison) | - | ✅ | ✅ | | | | | | Anthropic | [Claude Sonnet 4](https://docs.anthropic.com/en/docs/about-claude/models/overview) | ✅ | ✅ | ✅ | | | | | | Anthropic | [Claude Sonnet 4 w/Thinking](https://docs.anthropic.com/en/docs/about-claude/models/overview) | - | ✅ | ✅ | | | | | | Anthropic | [Claude Opus 4](https://docs.anthropic.com/en/docs/about-claude/models/overview) | - | - | ✅ | | | | | | Anthropic | [Claude Opus 4 w/Thinking](https://docs.anthropic.com/en/docs/about-claude/models/overview) | - | - | ✅ | | | | | | Google | [Gemini 1.5 Pro](https://deepmind.google/technologies/gemini/pro/) | ✅ | ✅ | ✅ (beta) | | | | | | Google | [Gemini 2.0 Flash](https://deepmind.google/technologies/gemini/flash/) | ✅ | ✅ | ✅ | | | | | | Google | [Gemini 2.0 Flash](https://deepmind.google/technologies/gemini/flash/) | ✅ | ✅ | ✅ | | | | | | Google | [Gemini 2.5 Pro Preview](https://cloud.google.com/vertex-ai/generative-ai/docs/models/gemini/2-5-pro) | - | ✅ | ✅ | | | | | | Google | [Gemini 2.5 Flash Preview](https://cloud.google.com/vertex-ai/generative-ai/docs/models/gemini/2-5-flash) (experimental) | ✅ | ✅ | ✅ | | | | | To use Claude 3 Sonnet models with Cody Enterprise, make sure you've upgraded your Sourcegraph instance to the latest version. ### Claude 3.7 and 4 Sonnet Claude 3.7 and 4 Sonnet have two variants; the base version, and the **extended thinking** version which supports deep reasoning and fast, responsive edit workflows. Cody enables using both, and lets the user select which to use in the model dropdown selector, so the user can choose whether to use extended thinkig depending on their work task. Claude 4 models support is available with Sourcegraph versions v6.4+ and v6.3.4167. It also depends on the deployement type and whether it's **thinking** or not. #### Claude 3.7 and 4 via Google Vertex, via AWS Bedrock Starting in Sourcegraph v6.4+ and v6.3.416, Claude 3.7 Extended Thinking - as well as Claude 4 base and extended thinking variants - are available in Sourcegraph when using Claude through either Google Vertex or AWS Bedrock. See [Model Configuration: Reasoning models](/cody/enterprise/model-configuration#reasoning-models) for more information. ## Autocomplete Cody uses a set of models for autocomplete which are suited for the low latency use case. | **Provider** | **Model** | **Free** | **Pro** | **Enterprise** | | | | | | :----------- | :---------------------------------------------------------------------------------------- | :------- | :------ | :------------- | --- | --- | --- | --- | | Fireworks.ai | [DeepSeek-Coder-V2](https://huggingface.co/deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct) | ✅ | ✅ | ✅ | | | | | | Anthropic | [claude Instant](https://docs.anthropic.com/claude/docs/models-overview#model-comparison) | - | - | ✅ | | | | | | | | | | | | | | | The default autocomplete model for Cody Free, Pro and Enterprise users is DeepSeek-Coder-V2. The DeepSeek model used by Sourcegraph is hosted by Fireworks.ai, and is hosted as a single-tenant service in a US-based data center. For more information see our [Cody FAQ](https://sourcegraph.com/docs/cody/faq#is-any-of-my-data-sent-to-deepseek). ## Smart Apply | **Provider** | **Model** | **Free** | **Pro** | **Enterprise** | | | | | | | | :----------- | :------------- | :------- | :------ | :------------- | --- | --- | --- | --- | --- | --- | | Fireworks.ai | Qwen 2.5 Coder | ✅ | ✅ | ✅ | | | | | | | Fireworks.ai is the default model for cody-gateway, but if you wish to switch to Claude models, Site admins can do it following these steps- Go to "Site admin" Click on the "Feature flags" Search for cody-smart-apply-instant-mode-enabled feature flag Turn off/delete the "cody-smart-apply-instant-mode-enabled" feature flag # Chat Query Types

This page lists all the query types that will return search results with the Sourcegraph chat.

Query types are only supported on Enterprise Starter and Enterprise plans. ## Symbol Search Symbol Search is a query type or workflow that helps developers understand how specific symbols (like functions, variables, or hooks) are used across a codebase by combining search results with contextual analysis through chat. It enables developers to explore and understand symbol usage patterns through a two-step process. First, developers search for a specific symbol (like `useCallback`) using Sourcegraph Chat, which returns relevant code snippets from across the codebase. These search results are automatically preserved as context for follow-up chat interactions. Developers can refine their understanding by selecting specific search results and asking natural language questions about the symbol's usage patterns. The chat will analyze the selected code context and provide a comprehensive summary of how the symbol is implemented and used throughout the codebase. ### Try it 1. Query for `= useCallback` 2. Select a subset of the search results which include usage for `useCallback` 3. Follow up and ask Sourcegraph chat `Summarize how useCallback is used` ### Example ## File Search Search is a query type or workflow that enables users to locate specific files across repositories and perform targeted analysis on the selected files through natural language follow-up queries. File Search simplifies finding and analyzing specific file types across your codebase. Unlike traditional code search, this workflow allows you to identify relevant files (for example, all `package.json` files), select the specific files you want to analyze, and then ask follow-up questions about the selected files. This two-step approach is particularly powerful when analyzing patterns or extracting information from similar files spread across multiple repositories. For instance, you could identify all configuration files, select the ones from relevant services, and then analyze their contents for inconsistencies or gather specific information through natural language queries. ### Try it 1. Type `package.json` 2. Check the checkbox next to the results you want to analyze 3. Follow up with `List all the dependencies used across these package.json files` ### Example ## String Literal Search String literal search is a query type or workflow that allows you to find exact text matches by enclosing your search term in quotes. This ensures precise matching instead of keyword-based results. When you enclose the text in quotes like `" authInfo: async provider =>"",` Sourcegraph performs an exact match search rather than keyword matching. After finding relevant files, you can select specific ones to analyze and ask follow-up questions about their contents - creating an interactive workflow combining precise search and contextual code analysis. ### Try it 1. Start a new chat with `"authInfo: async provider =>"` 2. Select a subset of the search results which include usage for the string 3. Follow up with `Summarize the contents` ### Example ## Error Lookups Error Lookups is a conversational workflow for Sourcegraph chat that helps developers understand error messages by providing plain-English explanations of when and why specific errors occur in their codebase and external service calls. Developers can use Error Lookups to understand error messages occurring in their codebase. When encountering an error, especially from external services or dependencies, developers can use chat to: 1. Search for specific error messages across their codebase 2. Select relevant files where the error is thrown or handled 3. Request a natural language explanation of the error's context, triggers, and implications The workflow combines Sourcegraph's code search capabilities with AI-powered analysis to provide developers with a clear, contextual understanding of error scenarios. Instead of having to dig through docs or source code manually, developers can quickly grasp the following: * The specific conditions that trigger the error * The underlying reasons for the error occurrence * Common scenarios where this error might appear * Potential approaches to handling or preventing the error This approach helps developers make more informed decisions about error handling and debugging, reducing the time spent deciphering cryptic error messages or searching through external docs. ### Try it * Query for `"unsupported platform"` * Select the file(s) where the error is thrown * Follow up with "Explain when and why this error is thrown" ### Demo
# Run Cody via proxies

This documentation helps you set up HTTP, HTTPS, and SOCKS proxies in VS Code and JetBrains IDEs. It also includes instructions for handling self-signed certificates on macOS and Windows.

You may be unable to authenticate with Cody or see a network reachability issue if you attempt to use Cody before configuring your proxy settings. ### Accessing the Proxy Settings 1. Open VS Code. 2. Navigate to the settings window of VS Code 3. In the search bar at the top of the Settings pane, type `proxy` 4. Click on `Edit in settings.json` to open the `settings.json` file ### Modifying `settings.json` for Proxy Configuration Add the following configuration fields in the `settings.json` file to set up your proxy: ```json { "http.proxy": "http://proxy_ip:port", // Replace with your proxy IP and port "http.proxySupport": "on", "http.proxyStrictSSL": true, "http.systemCertificates": true, "http.experimental.systemCertificatesV2": true } ``` ### Self-Signed Certificates in VS Code If your proxy uses self-signed certificates, ensure the following settings are enabled: ```json { "http.systemCertificates": true, "http.experimental.systemCertificatesV2": true } ``` ### Restart VS Code After modifying the `settings.json` file, close and restart VS Code to apply the new proxy settings. ### Supported Proxy Types VS Code supports HTTP, HTTPS, and SOCKS proxies. The `http.proxy` setting in `settings.json` will accommodate all three types based on the provided proxy URL. You may be unable to authenticate with Cody or see a network reachability issue if you attempt to use Cody before configuring your proxy settings. ### Accessing the Proxy Settings 1. Open your JetBrains IDE (e.g., IntelliJ IDEA, PyCharm) 2. Navigate to `Settings` by clicking on `File` > `Settings` (or `Preferences` on macOS) 3. In the search bar at the top, type `proxy` to find the proxy settings 4. Select the `HTTP` proxy or `SOCKS` proxy based on your requirement ### Configuring HTTP Proxy 1. In the proxy settings, choose `Manual Proxy Configuration` 2. Enter the following details: - **HTTP Proxy:** `http://proxy_ip:port` (Replace with your proxy IP and port) - **Enable proxy authentication if needed and provide credentials** 3. Click `Apply` and then `OK` to save the settings ### Configuring SOCKS Proxy 1. In the proxy settings, choose `Manual Proxy Configuration` 2. Enter the following details: - **SOCKS Proxy:** `socks5://proxy_ip:port` (Replace with your proxy IP and port) - **Enable proxy authentication if needed and provide credentials** 3. Click `Apply` and then `OK` to save the settings ### Using the Proxy with Cody 1. Open Cody settings within your JetBrains IDE 2. Connect to the Sourcegraph instance using the provided instance IP and token: - **Instance IP:** `http://instance_ip` - **Token:** `your_token` 3. Click `Apply`, then `OK`, and test the connection ### Supported Proxy Types JetBrains supports HTTP and SOCKS proxies. The `Manual Proxy Configuration` in the settings will accommodate both types based on the provided proxy URL. ### Known Limitations 1. Currently, only `Manual Proxy Configuration` is supported. The `auto-detect proxy settings` option is not fully supported. 2. HTTPS proxy is not supported in JetBrains currently ## Troubleshooting the Proxy Setup ### Basic Connectivity Test To verify that your proxy setup works, use the following `curl` command in your terminal, replacing `proxy_ip` and `instance_ip` with your actual proxy and instance IP addresses: ```bash curl -x http://proxy_ip:port http://instance_ip ``` This command should return a response from the instance, confirming that the proxy is correctly routing your requests. ## Adding Self-Signed Certificates ### For macOS 1. Download the self-signed certificate. 2. Open `Keychain Access` and import the certificate. 3. Ensure the certificate is added by searching for the proxy IP. 4. Set the certificate to `Always Trust`. ### For Windows 1. Download the self-signed certificate. 2. Open `Manage User Certificates` by typing it in the Windows search bar. 3. Import the certificate into the `Trusted Root Certification Authorities` store. 4. Confirm that the certificate is listed and trusted. Please confirm with your IT Administrator to ensure you trust the right certificate. When adding self-signed certificates, try making a `curl` request routed through the HTTPS proxy to check if the computer uses the self-signed certificate. Your request will look something like: ```bash curl -x https://proxy_ip https://destination_ip ``` ### Additional Support Please get in touch with the Cody support team if you need further assistance with proxy configurations, especially for special enterprise setups. They can provide guidance tailored to your specific network environment.
# Prompts

Learn how prompts can automate and accelerate your workflow with Cody.

Cody offers quick, ready-to-use **Prompts** to automate key tasks in your workflow. Prompts are created and saved in the **Prompt Library** and can be accessed from the top navigation bar in the Sourcegraph.com instance. To run Prompts and access Prompt Library, you must have the following: - Free account on Sourcegraph.com or Sourcegraph Enterprise instance with Cody enabled - Cody extension installed in your IDE (VS Code, JetBrains, Visual Studio) ## Prompt Library The **Prompt Library** allows you to create, edit, share, and save prompts you’ve created or shared within your organization. You can also search for prompts, filter the list to find a specific prompt by the owner, and sort by name or updated recently. Go to **Tools > Prompt Library** from the top navigation bar in the Sourcegraph.com instance. Alternatively, you can access the **Prompt Library** from the **Cody** extension in your IDE, which directs you to the Prompt Library page. Here, you can view all prompts (shared with you in an organization or created by you) and some core (built-in) prompts to help you get started. ![prompt-library-overview](https://storage.googleapis.com/sourcegraph-assets/Docs/prompt-library-overview-2025.png) ### Core (built-in) prompts The core (built-in) prompts are available to all users. These prompts are designed to help you start with Cody and provide a starting point for your own prompts. You get the following core (built-in) prompts: - document-code - explain-code - find-code-smells - generate-unit-tests You can run these prompts by clicking the **play** icon next to the prompt name, **copy the prompt permalink**, or **duplicate the prompt** to make a copy and edit it according to your needs. ## Create prompts Click the **New prompt** button from the **Prompt Library** page. - Select the **Owner** and **Prompt Name** - Write a prompt description - Next, fill out the **Prompt template** box with all your prompt instructions - You can also add dynamic context that will allow your prompt to use content from different sources like current selection and current file - Select the visibility of the prompt, either **Public** or **Private** - You can mark your prompt as **draft**. Draft prompts are not visible to other users until you publish it - Choose the mode of the prompt, whether it will be **Chat only** or can **Edit code** - Once done, click the **Create prompt** button There are also a few advanced options that you can configure. ![create-prompt](https://storage.googleapis.com/sourcegraph-assets/Docs/create-prompts-0225.jpg) ### Draft prompts You can mark your prompt as a draft. A draft prompt is not visible to everyone. You can only view, run, and edit your draft prompts until you publish it. ## View prompts The new prompt is added to the Prompt Library page and appears in the Prompts list in the Cody chat panel (both in the editor and on the web). If your prompt does not appear in the Prompts list, make sure you've logged in with the same account you used to create it. Once the prompt is visible, it's ready to be used by: - **The prompt's owner** if it is a user - **All members of the organization** if the prompt's owner is an organization - **Everyone** if the prompt is marked **Public** (which only site admins can do) ![prompts-in-vscode](https://storage.googleapis.com/sourcegraph-assets/Docs/prompt-in-vscode-2025.jpg) ## Edit prompts To edit a prompt, click the Edit button next to the prompt in the Prompt Library and make the necessary changes. You can also use this interface to **Transfer Ownership** of the prompt or delete it from this view. ## Prompt tags Prompt tags are supported in Sourcegraph Enterprise, and only site admins can create them. Site admins can categorize prompts and create **tags**. This helps you keep your prompts organized, especially when there are many prompts to use and manage. - Go to **Tools > Prompt Library** from your Enterprise Sourcegraph instance - From the left sidebar click **Manage** next to the **tags** list - All the new and existing tags are listed here. You can also search for a specific tag by typing its name in the search box - Click **New tag**, type the **Tag name** and hit the **Create** button ![prompt-tags](https://storage.googleapis.com/sourcegraph-assets/Docs/prompts-tags-2025.png) Once created, it will appear in the **Tags** list and in the sidebar of the Prompt Library. To manage your prompt tags, you can **edit**, **delete**, or **copy link to prompts with this tag**. ### Assigning tags to prompts Once site admins create tags, other users in your organization can assign tags to their prompts. While creating or editing a prompt, there is a drop-down selector **Add prompt to...** to assign tags to the prompt. ![assign-prompt-tags](https://storage.googleapis.com/sourcegraph-assets/Docs/assign-prompt-tags-2025.png) You can assign multiple tags to a prompt and group them based on their functionality, category, or any other criteria for your organization. In addition, with tags assigned to prompts, you can filter prompts by tags in the Prompt Library. ## Specific and dynamic context Sourcegraph 6.0 adds Beta support for the `@` mention menu in the prompt library. When writing prompts, you can leverage both specific and dynamic context through the `@` mention system. ![A dropdown appears after typing `@` in the prompt editor.](https://storage.googleapis.com/sourcegraph-assets/Docs/%40-mention-prompts-0225.jpg) Type `@` to open a dropdown menu that lets you reference specific context like symbols, directories, files, repositories and web URLs. When selecting a web URL, type out the domain, including the `https://` prefix, for example, https://sourcegraph.com. For dynamic context that adapts based on what the user is working on, the prompt editor provides special mentions for the current selection, current file, current repository, current directory, and open tabs. When a user runs a prompt template containing dynamic context mentions, they are automatically resolved to the appropriate specific context based on the user's current workspace state. To add dynamic context, click on one of the buttons below the prompt editor. We will soon move the buttons into the `@` mention menu as well. This powerful combination allows prompt authors to create templates that can intelligently access both explicitly defined context and contextually relevant information at runtime. ## Run prompts You can run prompts via: - Clicking the play icon next to a prompt in the Prompt Library list - From the **Prompts** dropdown in the Cody chat panel both in the editor and on the web However, prompts marked with the **Edit code** mode can only run from the IDE editor. You cannot run these prompts from the Prompt Library in the web UI. For example, the **document-code** built-in prompt can only run the IDE editor. ![run-prompt-web](https://storage.googleapis.com/sourcegraph-assets/Docs/run-prompt-web-2025.png) ## Promoted prompts Promoted prompts are supported in VS Code and Cody Web. **Site admins** can promote a prompt by enabling the **Promote prompt** checkbox. This setting allows admins to highlight prompts they want users to use. Promoted prompts are marked with an icon next to their name and appear at the top of the prompt list in the Cody chat. ![icon-recommended-prompts](https://storage.googleapis.com/sourcegraph-assets/Docs/recommended-prompts-icon-2025.png) Prompt Library replaces the default and custom commands in Cody. Custom Commands (legacy) have been deprecated and will no longer be supported. If you have custom commands, VS Code automatically detects them and notifies you to migrate them to the Prompts Library. There is a **Migrate commands** button in the Cody chat panel. Click it, and your custom commands will be migrated to the Prompts Library. Once migrated, you can run it like any other prompt. However, you **cannot** edit the migrated custom command in the Prompts Library.
# OpenCtx Context Providers

Use additional context sources from outside of your codebase by leveraging OpenCtx providers.

MCP is the recommended method for adding external context in Cody due to its broad community adoption and extensive tool support. [Read the docs](/cody/capabilities/agentic-context-fetching#mcp-support) to learn more about configuring MCP. [OpenCtx](https://openctx.org/) is an open standard for bringing contextual info about code into your dev tools. OpenCtx context providers are in the Experimental stage for all Cody users. Enterprise users can use this, but with limited support. Cody Free and Pro users can use OpenCtx providers to fetch and use context from the following sources: - [Webpages (via URL)](https://openctx.org/docs/providers/web) (enabled in Cody by default) - [Jira tickets](https://openctx.org/docs/providers/jira) - [Linear issues](https://openctx.org/docs/providers/linear-issues) - [Notion pages](https://openctx.org/docs/providers/notion) - [Google Docs](https://openctx.org/docs/providers/google-docs) - [Slack](https://openctx.org/docs/providers/slack) - [Storybook](https://openctx.org/docs/providers/storybook) - [Sourcegraph code search](https://openctx.org/docs/providers/sourcegraph-search) ## Enable OpenCtx context providers OpenCtx context providers are only supported with Cody VS Code extension. To try OpenCtx context providers with Cody, you must add to your VS Code extension settings. Go to Cody Settings, and check the box for **OpenCtx: Enable**. This will enable OpenCtx. You don't need the OpenCtx VS Code extension to use context fetching with OpenCtx. You should uninstall the extension before using this feature in Cody. For example, to use the [DevDocs provider](https://openctx.org/docs/providers/devdocs), add the following to your `settings.json`: ```json "openctx.providers": { "https://openctx.org/npm/@openctx/provider-devdocs": { "urls": ["https://devdocs.io/go/", "https://devdocs.io/angular~16/"] } }, ``` Please refer to the [context providers docs](https://openctx.org/) for instructions and examples of configuring other providers such as Confluence, Jira, GitHub, Slack, Notion, Linear Issues, Linear Docs, Google Docs, and more.
# Supported local Ollama models with Cody {/* Internal docs only. Offloading from our production docs for now. */} Compatibility with Ollama is currently in the Experimental stage and is available for Cody Free and Pro plans. The support is for Ollama is limited feel free to contact us for any questions or feedback. ## Cody Autocomplete with Ollama To get autocomplete suggestions from Ollama locally, follow these steps: - Install and run [Ollama](https://ollama.ai/) - Download one of the supported local models using `pull`. The `pull` command is used to download models from the Ollama library to your local machine. - `ollama pull deepseek-coder-v2` for [deepseek-coder](https://ollama.com/library/deepseek-coder-v2) - `ollama pull codellama:13b` for [codellama](https://ollama.ai/library/codellama) - `ollama pull starcoder2:7b` for [starcoder2](https://ollama.ai/library/starcoder2) - Update Cody's VS Code settings to use the `experimental-ollama` autocomplete provider and configure the right model: ```json "cody.autocomplete.advanced.provider": "experimental-ollama", "cody.autocomplete.experimental.ollamaOptions": { "url": "http://localhost:11434", "model": "deepseek-coder-v2" } ``` - Confirm Cody uses Ollama by looking at the Cody output channel or the autocomplete trace view (in the command palette) ## Cody chat with Ollama Chat model selector To generate chat with Ollama locally, follow these steps: - Download [Ollama](https://ollama.com/download) - Start Ollama (make sure the Ollama logo is showing up in your menu bar) - Select a chat model (model that includes instruct or chat, for example, [gemma:7b-instruct-q4_K_M](https://ollama.com/library/gemma:7b-instruct-q4_K_M)) from the [Ollama Library](https://ollama.com/library) - Pull (download) the chat model locally (for example, `ollama pull gemma:7b-instruct-q4_K_M`) - Once the chat model is downloaded successfully, open Cody in VS Code - Open a new Cody chat - In the new chat panel, you should see the chat model you've pulled in the dropdown list - Currently, you will need to restart VS Code to see the new models You can run `ollama list` in your terminal to see what models are currently available on your machine. ### Run Cody offline with local Ollama models You can use Cody with or without an internet connection. The offline mode does not require you to sign in with your Sourcegraph account to use Ollama. Click the button below the Ollama logo and you'll be ready to go. ![offline-cody-with-ollama](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-offline-ollama.jpg) You still have the option to switch to your Sourcegraph account whenever you want to use Claude, OpenAI, Gemini, etc. # Cody Capabilities

Cody offers a rich set of capabilities and features that help you write better code faster. Learn and understand more about Cody's features and core AI functionality in this section.

# Manage Cody Context

You can control and manage what context from your codebase is used by Cody. You can do this by using Cody Context Filters.

Cody Context Filters is available only for Enterprise users on all supported [clients](/cody/clients). ## Context Filters Admins on the Sourcegraph Enterprise instance can use the Cody Context Filters to determine which repositories Cody can use as the context in its requests to third-party LLMs. You can use Context Filters if you have: - A valid Cody Enterprise license running on Sourcegraph instance version `>=5.4.0` - Running the supported Cody client versions: VS Code `>=1.20.0` and JetBrains `>=6.0.0` - Setting the `cody-context-filters-enabled` feature flag to `true` Site admins can customize the `cody.contextFilters` field in the site configuration to specify which repositories Cody should `include` or `exclude`, using the following structure: ```json { // Optional. If defined, it must have at least one // field set (either `include` or `exclude`). "cody.contextFilters": { // Optional. If defined, it must have at least one item. "include": [ { // Required. Should follow the RE2 syntax. "repoNamePattern": "^github\\.com\\/sourcegraph\\/.*" } ], // Optional. If defined, it must have at least one item. "exclude": [ { // Required. Should follow the RE2 syntax. "repoNamePattern": ".*analytics.*" } ] } } ``` ## How `include` and `exclude` rules work The `include` and `exclude` rules define the repositories Cody can use as context. The rules can be defined in the following combination: ### `cody.contextFilters` field is not defined By default, Cody can access all repositories for context when making requests to third-party LLMs, with no restrictions on inclusion or exclusion. ### Only `include` rules are specified Cody is restricted to using content only from repositories whose names match the patterns specified in the `include` field. No repositories are explicitly excluded if no `exclude` rules are defined. The `include` field can also contain a catch-all pattern (e.g., a `regexp` that matches any string), allowing Cody to access content from all repositories. A single match in the `include` rules is enough for a repository to be included. For example: ```json { "cody.contextFilters": { // Only repositories whose names either start with "github.com/sourcegraph/" // or contain "cody" are allowed. "include": [ { "repoNamePattern": "^github\\.com\\/sourcegraph\\/.+" }, { "repoNamePattern": ".*cody.*" } ] } } ``` These `include` rules allow Cody only to utilize the repository matching the `regexp` with full access to fetch context. ### Only `exclude` rules are specified By default, all repositories are included unless specific `include` rules are defined. If **only** `exclude` rules are specified, Cody will not use content from any repository whose name matches at least one pattern in the `exclude` field. The `exclude` field can also include a catch-all condition, such as a `regexp` that matches any string. When such a condition is present, Cody is entirely restricted from accessing content from any repository. For example: ```json { "cody.contextFilters": { // All repositories are allowed except for "github.com/sourcegraph/cody-analytics" // or the ones containing "secret" in their name. "exclude": [ { "repoNamePattern": "^github\\.com\\/sourcegraph\\/cody-analytics$" }, { "repoNamePattern": ".*secret.*" } ] } } ``` #### Chat behavior when exclude rules are defined Some Cody features, such as using **Prompts**, will not work if the `exclude` rules are defined. They appear to be disabled in the Cody chat interface. If you try running any of these, you'll be prompted with an error message. However, Cody chat will still work; you can use it to ask questions. ### Both `include` and `exclude` rules are specified When requesting third-party LLMs, Cody can use content from a repository if its name matches any of the `include` patterns and does not match any of the `exclude` patterns. Thus, the `exclude` rules filter only the repositories allowed by the `include` rules. For example, ```json { "cody.contextFilters": { // All repositories starting with "github.com/sourcegraph/" are allowed "include": [ { repoNamePattern: "^github\\.com\\/sourcegraph\\/.+" } ], // except for "github.com/sourcegraph/cody-analytics" // and the ones containing "cody" in their name. "exclude": [ { "repoNamePattern": "^github\\.com\\/sourcegraph\\/cody-analytics$" }, { "repoNamePattern": ".*cody.*" } ] } } ``` In this case, Cody can access content from repositories whose names start with `github.com/sourcegraph/`. However, it excludes files from repositories with `cody` in their name. ## Cody Context Filters Compatibility Matrix Cody Context Filters work only on Sourcegraph versions `>=5.4.0`, VS Code `>=1.20.0` and JetBrains `>=6.0.0` for Enterprise users. Depending on the client type, here's a breakdown of versions supported and the behavior for unsupported versions: | **Client** | Client version | Sourcegraph `v<5.4.0` | Sourcegraph `v=5.4.0` | Sourcegraph `v=5.4.12303` | Sourcegraph `v>=5.4.0` | | ------------- | ------------------------- | --------------------- | --------------------- | ------------------------------- | ---------------------- | | **VS Code** | `< 1.16.X` | N/A | Cody not compatible | Respects policy, ask to upgrade | Not affected | | | `1.18.0 <` and `< 1.20.0` | N/A | Cody not compatible | Respects policy, ask to upgrade | Not affected | | | `>= 1.20.0` | N/A | Respects policy | Respects policy | Not affected | | **JetBrains** | `<= 5.5.9` | N/A | Cody not compatible | Respects policy, ask to upgrade | Not affected | | | `5.5.10 <=` and `< 6.0.0` | N/A | Cody not compatible | Respects policy, ask to upgrade | Not affected | | | `>= 6.0.0` | N/A | Respects policy | Respects policy | Not affected |
# Debug Code

Learn how Cody helps you identify errors in your code and provides code fixes.

Cody is optimized to identify and fix errors in your code. Its debugging capability and autocomplete suggestions can significantly accelerate your debugging process, increasing developer productivity. All Cody IDE extensions (VS Code, JetBrains) support code debugging and fixes capabilities. ## Use chat for code fixes When you encounter a code error, you can use the chat interface and ask Cody about it. You can paste the faulty code snippets directly in the chat window, and Cody will provide a fix. The suggestions can be a corrected code snippet you can copy and paste into your code. Or you can ask a follow-up question for additional context to help debug the code. Moreover, you can paste an error message in the chat and ask Cody to provide a list of possible solutions. Let's look at a simple example to understand how Cody can help you debug your code. The following code snippet should print the sum of two numbers. ```js function sum(a, b) { var result = a + b; console.log('The sum is: ' + $result); } sum(3 , 4); ``` When you try to `console.log` the `result`, it does not print the correct summed value. Cody can help you both identify the error and provide a solution to fix it. Let's debug the code snippet. Paste the code snippet inside the Cody chat window and ask Cody to fix it. In addition, Cody helps you reduce the chances of getting syntax and typo errors. The Cody IDE extensions provide context-aware suggestions based on your codebase, helping you avoid common mistakes and reduce debugging time. ## Detecting code smell Cody can detect early code smells to ensure coding best practices and quality and provide suggestions to improve your code. By detecting such potential errors early on, you can avoid scalability and code maintainability issues that might arise in the future. You can detect code smells by the **find-code-smells** prompt from the Prompts drop-down menu in the chat panel. If you want to refine your debugging process, you can create a new prompt from the Prompt Library and use it to debug your code. ## Code Actions Code Actions are available only in Cody VS Code extension. When you make a mistake while writing code, Cody's **Code Actions** come into play and a red warning triggers. Along with this, you get a lightbulb icon. If you click on this lightbulb icon, there is an **Ask Cody to fix** option. - Click the lightbulb icon in the project file - Select **Ask Cody to fix** option - **Cody is working** notice will appear and provide a quick fix that you can **Accept**, **Reject**, or **Open Diff** view to see the changes ![code-actions](https://storage.googleapis.com/sourcegraph-assets/Docs/cody-code-actions-vscode-1124.png)
# Chat

Chat with the AI assistant in your code editor or via the Sourcegraph web app to get intelligent suggestions, code autocompletions, and contextually aware answers.

You can **chat** with Cody to ask questions about your code, generate code, and edit code. By default, Cody has the context of your open file and entire repository, and you can use `@` to add context for specific files, symbols, remote repositories, or other non-code artifacts. You can do it from the **chat** panel of the supported editor extensions ([VS Code](/cody/clients/install-vscode), [JetBrains](/cody/clients/install-jetbrains), [Visual Studio](/cody/clients/install-visual-studio)) or in the [web](/cody/clients/cody-with-sourcegraph) app. ## Prerequisites To use Cody's chat, you'll need the following: - [Sourcegraph Enterprise Starter](https://sourcegraph.com/pricing) or [Enterprise account](https://sourcegraph.com/pricing) - A supported editor extension [VS Code](https://marketplace.visualstudio.com/items?itemName=sourcegraph.cody-ai), [JetBrains](https://plugins.jetbrains.com/plugin/9682-cody-ai-coding-assistant-with-autocomplete--chat) installed or use via Web app ## How does chat work? Cody answers questions by searching your codebase and retrieving context relevant to your questions. Cody uses several methods to search for context, including Sourcegraph's native search and keyword search. Finding and using context allows Cody to make informed responses based on your code rather than being limited to general knowledge. When Cody retrieves context to answer a question, it will tell you which code files it reads to generate its response. Cody can assist you with various use cases, such as: - Generating an API call: Cody can analyze your API schema to provide context for the code it generates - Locating a specific component in your codebase: Cody can identify and describe the files where a particular component is defined - Handling questions that involve multiple files, like understanding data population in a React app: Cody can locate React component definitions, helping you understand how data is passed and where it originates ## Chat features There are several features that you can use to make your chat experience better. These features may vary depending on the [client](/cody/clients) you are using. You can learn more about the support for these functionalities in the [feature parity reference](/cody/clients/feature-reference#chat). ## Default context When you start a new Cody chat, the input window opens with a default `@-mention` context chips for the opened file and the current repository. ![context-retrieval](https://storage.googleapis.com/sourcegraph-assets/Docs/context-retrieval-repo-file-2025.png) At any point in time, you can edit these context chips or remove them entirely if you do not want to use these as context. Any chat without a context chip will instruct Cody to use no codebase context. However, you can always provide an alternate `@-mention` file or symbols to let Cody use it as a new context source. When you have both a repository and files @-mentioned, Cody will search the repository for context while prioritizing the mentioned files. ## Add new context You can add new custom context by adding `@-mention` context chips to the chat. At any point, you can use `@-mention` a repository, file, line range, or symbol, to ask questions about your codebase. Cody will use this new context to generate contextually relevant code. ## OpenCtx context providers OpenCtx context providers are in the Experimental stage for all Cody VS Code users. Enterprise users can also use this but with limited support. If you have feedback or questions, please visit our [support forum](https://community.sourcegraph.com/c/openctx/10). [OpenCtx](https://openctx.org/) is an open standard for bringing contextual info about code into your dev tools. Cody Free and Pro users can use OpenCtx providers to fetch and use context from the following sources: - [Webpages](https://openctx.org/docs/providers/web) (via URL) - [Jira tickets](https://openctx.org/docs/providers/jira) - [Linear issues](https://openctx.org/docs/providers/linear-issues) - [Notion pages](https://openctx.org/docs/providers/notion) - [Google Docs](https://openctx.org/docs/providers/google-docs) - [Sourcegraph code search](https://openctx.org/docs/providers/sourcegraph-search) You can use `@-mention` web URLs to pull live information like docs. You can connect Cody to OpenCtx to `@-mention` non-code artifacts like Google Docs, Notion pages, Jira tickets, and Linear issues. ## LLM selection Cody allows you to select the LLM you want to use for your chat, which is optimized for speed versus accuracy. Cody Free and Pro users can select multiple models. Enterprise users with the new [model configuration](/cody/clients/model-configuration) can use the LLM selection dropdown to choose a chat model. You can read about these supported LLM models [here](/cody/capabilities/supported-models#chat-and-commands). ![LLM-models-for-cody-free](https://storage.googleapis.com/sourcegraph-assets/Docs/llm-dropdown-options-free-pro-2025.png) ## Smart Apply and Execute code suggestions Cody lets you dynamically insert code from chat into your files with **Smart Apply**. Whenever Cody provides a code suggestion, you can click the **Apply** button. Cody will then analyze your open code file, find where that relevant code should live, and add a diff. For chat messages where Cody provides multiple code suggestions, you can apply each in sequence to go from chat suggestions to written code. Smart Apply also supports the executing of commands in the terminal. When you ask Cody a question related to terminal commands, you can now execute the suggestion in your terminal by clicking the `Execute` button in the chat window. ![smart-apply](https://storage.googleapis.com/sourcegraph-assets/Docs/smart-apply-2025.png) ### Model used for Smart Apply To ensure low latency, Cody uses a more targeted Qwen 2.5 Coder model for Smart Apply. This model improves the responsiveness of the Smart Apply feature in both VS Code and JetBrains while preserving edit quality. Users on Cody Free, Pro, Enterprise Starter, and Enterprise plans get this default Qwen 2.5 Coder model for Smart Apply suggestions. Enterprise users not using Cody Gateway get a Claude Sonnet-based model for Smart Apply. ## Chat history Cody keeps a history of your chat sessions. You can view it by clicking the **History** button in the chat panel. You can **Export** it to a JSON file for later use or click the **Delete all** button to clear the chat history. ## Prompts Cody offers quick, ready-to-use [prompts](/cody/capabilities/commands) for common actions to write, describe, fix, and smell code. Read more about [prompts](/cody/capabilities/commands) here. ## Ask Cody to write code Cody chat can also write code for your questions. For example, in VS Code, ask Cody to "write a function that sorts an array in ascending order". You are provided with code suggestions in the chat window and the following options for using the code. - The **Copy Code** icon to your clipboard and paste the code suggestion into your code editor - Insert the code suggestion at the current cursor location by the **Insert Code at Cursor** icon - The **Save Code to New File** icon to save the code suggestion to a new file in your project If Cody's answer isn't helpful, you can try asking again with a different context: - **Public knowledge only**: Cody will not use your own code files as context; it’ll only use knowledge trained into the base model. - **Current file only**: Re-run the prompt again using the current file as context. - **Add context**: Provides @-mention context options to improve the response by explicitly including files, symbols, remote repositories, or even web pages (by URL). The enhanced chat experience input can be accessed from the chat panel of the supported editor extensions (VS Code and JetBrains) and the web app. It combines light code search, AI-powered chat, and agentic capabilities into a unified developer interface. It's designed to accelerate the entire developer workflow by providing a more intuitive and powerful way to interact with code. ## Prerequisites To use Cody's chat, you'll need the following: - [Sourcegraph Enterprise Starter](https://sourcegraph.com/pricing) or [Enterprise account](https://sourcegraph.com/pricing) - A supported editor extension [VS Code](https://marketplace.visualstudio.com/items?itemName=sourcegraph.cody-ai), [JetBrains](https://plugins.jetbrains.com/plugin/9682-cody-ai-coding-assistant-with-autocomplete--chat) installed or use via Web app ## Key features The enhanced chat experience includes everything in the Free plan, plus the following: ## Smart search integration The smart search integration enhances Sourcegraph's chat experience by providing lightweight code search capabilities directly within the chat interface. This feature simplifies developer workflows by offering quick access to code search without leaving the chat environment. The integration delivers personalized search results ranked by your contribution history, with frequently accessed repositories appearing higher in results. Users can view code snippets with relevant context and open files directly in their editor. Search results automatically become available as context for follow-up queries, with flexible controls for selecting which results to include. While optimized for keyword-style queries and searching across a few repositories, this integration complements rather than replaces the full [Code Search](/code-search) product, which remains the recommended tool for comprehensive enterprise-wide code search. ## Context-aware responses Search results generated through smart search integration can be automatically used as context for follow-up queries. Here's what happens with each scenario: ### Search responses * Search results can be used directly as context for follow-up queries * Users can select which search results to include as context using checkboxes * By default, all search results are added as context * A context chip shows the number of search results being used (e.g., "10 code search results") * Users can remove the context chip if they don't want to use it for follow-ups ### Chat responses Executing terminal commands for additional context is an experimental feature. * Performs background searches for context * Retrieves full context from files, symbols, remote repos, and web pages * Can execute terminal commands (with permission) for additional context * Creates personal notes usable across chat sessions * Pulls in [OpenCtx](/cody/capabilities/openctx) providers for additional context ## How does chat work? The following is a general walkthrough of the chat experience: 1. The user enters a query in the chat interface 2. By default a user gets a chat response for the query 3. To get integrated search results, toggle to **Run as search** from the drop-down selector or alternatively use `Cmd+Opt+Enter` (macOS) 4. For search: - Displays ranked results with code snippets - Shows personalized repository ordering - Provides checkboxes to select context for follow-ups 5. For chat: - Delivers AI-powered responses - Can incorporate previous search results as context 6. Users can: - Switch between search and chat modes - Click on results to open files in their editor - Ask follow-up questions using selected context - Use `@` to add context for specific files, symbols, remote repositories, or other non-code artifacts
# Autocomplete

Learn how Cody helps you get contextually-aware autocompletion for your codebase.

Cody predicts what you're trying to write before you even type it. It offers single-line and multi-line suggestions based on the provided code context, ensuring accurate autocomplete suggestions. Cody autocomplete supports a [wide range of programming languages](/cody/faq#what-programming-languages-does-cody-support) because it uses LLMs trained on broad data. Code autocompletions are optimized for both server-side and client-side performance, ensuring seamless integration into your coding workflow. The **default** autocomplete model for Cody Free, Pro, and Enterprise users is **[DeepSeek V2](https://huggingface.co/deepseek-ai/DeepSeek-V2)**, which significantly helps boost both the responsiveness and accuracy of autocomplete. ## Cody's autocomplete capabilities The autocompletion model is designed to enhance speed, accuracy, and the overall user experience that offers: - **Increased speed and reduced latency**: The P75 latency is reduced by 350 ms, making the autocomplete function faster - **Improved accuracy for multi-line completions**: Completions across multiple lines are more relevant and accurately aligned with the surrounding code context - **Higher completion acceptance rates**: The average completion acceptance rate (CAR) is improved by more than 4%, providing a more intuitive user interaction ## How does autocomplete work? First, you'll need the following setup: - A Free or Pro account via Sourcegraph.com or a Sourcegraph Enterprise instance - A supported editor extension (VS Code, JetBrains, Visual Studio) The autocomplete feature is enabled by default on all IDE extensions, i.e., VS Code and JetBrains. Generally, there's a checkbox in the extension settings that confirms whether the autocomplete feature is enabled or not. In addition, some autocomplete settings are optionally and explicitly supported by some IDEs. For example, JetBrains IDEs have settings that allow you to customize colors and styles of the autocomplete suggestions. When you start typing, Cody will automatically provide suggestions and context-aware completions based on your coding patterns and the code context. These autocomplete suggestions appear as grayed text. Press the `Enter` or `Tab` to accept the suggestion. ## Configure autocomplete on an Enterprise Sourcegraph instance Users with admin access can only configure autocomplete settings. By default, a fully configured Sourcegraph instance picks a default LLM to generate code autocomplete. Custom models can be used for Cody autocomplete based on your specific requirements. To do so: - Go to the **Site admin** of your Sourcegraph instance - Navigate to **Configuration > Site configuration** - Here, edit the `completionModel` option inside the `completions` - Click the **Save** button to save the changes Cody supports and uses a set of models for autocomplete. Learn more about these [here](/cody/capabilities/supported-models#autocomplete). It's also recommended to read the [Enabling Cody on Sourcegraph Enterprise](/cody/clients/enable-cody-enterprise) docs. ## LLM models for autocomplete Cody uses a set of models for autocomplete. Learn more about these [here](/cody/capabilities/supported-models#autocomplete).
# Auto-edit

Auto-edit suggests code changes by analyzing cursor movements and typing. After you've made at least one character edit in your codebase, it begins proposing contextual modifications based on your cursor position and recent changes.

Auto-edit is currently supported with Sourcegraph v6.0+ for Pro, Enterprise Starter, and Enterprise accounts on Cody Gateway. Auto-edit requires Fireworks to be enabled as a provider. Enterprise customers without Fireworks enabled can disable the feature flag. ## Capabilities of auto-edit - Can automatically insert and delete text - Can modify code before and after the cursor position - Works with repetitive tasks, especially in test files - Can propose similar changes across multiple locations based on recent edits - Can handle complex refactoring operations like adding parameters and unpacking functions ## How does auto-edit work? The system detects potential changes based on the following: - Cursor placement in the code - Recent changes made in similar contexts - Function usage patterns - Common refactoring patterns ## Auto-edit vs. Autocomplete In terms of feature set and functionality, auto-edit and autocomplete have some key differences: | **Feature** | **Autocomplete** | **Auto-edit** | | ---------------------- | -------------------------------------- | -------------------------------------------------- | | **Text modification** | Can only insert text, not delete | Can both insert and delete text | | **Cursor position** | Only proposes content after the cursor | Can modify code before and after the cursor | | **Suggestion trigger** | Suggests on every character typed | Proposes changes based on context and recent edits | ## Use cases The auto-edit feature can help you with various repetitive tasks in your code: - **Function signature refactoring**: Automatically suggests adding error returns, new parameters, or changing return types across function definitions. For example, when you modify a function to return both a value and an error, auto-edit helps update the function signature. - **Call site updates**: When you change a function's signature, auto-edit detects all locations where the function is called and suggests necessary modifications to match the new signature. This includes updating parameter orders, adding error handling, and adjusting return value usage. - **Test file maintenance**: Helps with repetitive updates in test files, such as modifying test assertions, updating mock objects, or changing test data structures. Auto-edit recognizes patterns from your recent changes and suggests similar modifications across related tests. - **Parameter refactoring**: Assists in adding, removing, or reorganizing function parameters. When you unpack a function to handle more cases, auto-edit helps restructure the parameter list and suggests corresponding changes at call sites. - **Type system modifications**: Auto-edit identifies and suggests consistent changes across your codebase when updating type definitions or interfaces. This includes updating variable declarations, function parameters, and return types to maintain type consistency. Auto-edit is supported by both Cody VS Code and JetBrains plugins. Auto-edit for VS Code is currently in Beta. It's available for Pro, Enterprise Starter, and Enterprise users on Cody Gateway. Auto-edit requires Fireworks to be enabled as a provider. Enterprise customers without Fireworks enabled can disable the feature flag. ## Enabling auto-edit in VS Code Auto-edit is enabled by default for Cody Pro Enterprise Starter and Enterprise users. You can opt out and switch back to autocomplete by selecting it from the suggestion mode in the Cody VS Code extension settings. Site admins can opt their organization out of the auto-edit feature by disabling it from their config settings. ### Auto-edit access for Enterprise customers Auto-edit is available for Enterprise customers with [Sourcegraph Cody Gateway](/cody/core-concepts/cody-gateway#sourcegraph-cody-gateway) access. Enabling the feature requires two steps: 1. Site administrators must: - Ensure the feature flag `cody-autoedit-experiment-enabled-flag` is enabled (enabled by default) - Add `fireworks::*` as an [allowed provider](https://sourcegraph.com/docs/cody/enterprise/model-configuration#model-filters) (see below) 2. Once enabled, auto-edit will become the default suggestion mode for all users 3. Users can optionally switch back to autocomplete from the Cody extension settings 4. Site admins can opt out of auto-edits using the `cody-autoedit-experiment-enabled-flag` feature flag The following example demonstrates how to add Fireworks as an allowed LLM provider: ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": { "modelFilters": { // Only allow "beta" and "stable" models. // Not "experimental" or "deprecated". "statusFilter": ["beta", "stable"], // Allow any models provided by Anthropic, OpenAI, Google and Fireworks. "allow": [ "anthropic::*", // Anthropic models "openai::*", // OpenAI models "google::*", // Google Gemini models "fireworks::*", // Open source models hosted by Sourcegraph ], } } } ``` Auto-edit for JetBrains IDEs is Experimental and supports JetBrains versions 7.84.0+. It's available for Pro, Enterprise Starter, and Enterprise users on Cody Gateway. Auto-edit requires Fireworks to be enabled as a provider. Enterprise customers without Fireworks enabled can disable the feature flag. ## Enabling auto-edit in JetBrains You can opt-in the auto-edit feature from the JetBrains Cody plugin settings. - Click the three-dot menu on the top right of the Cody plugin window and select **Open Cody Settings Editor** - This will open the `cody_settings.json` file in your editor - Here, add `"cody.suggestions.mode": "auto-edit"` setting to enable the auto-edit feature ![JetBrains-Cody-Settings-Editor](https://storage.googleapis.com/sourcegraph-assets/Docs/jb-cody-settings.png) ### Auto-edit access for Enterprise customers Auto-edit is available for Enterprise customers with [Sourcegraph Cody Gateway](/cody/core-concepts/cody-gateway#sourcegraph-cody-gateway) access. Enabling the feature requires two steps: 1. Site administrators must: - Ensure the feature flag `cody-autoedit-experiment-enabled-flag` is enabled from the settings editor - Add `fireworks::*` as an [allowed provider](https://sourcegraph.com/docs/cody/enterprise/model-configuration#model-filters) (see below) 2. Once enabled, auto-edit will become the default suggestion mode for all users 3. Users can optionally switch back to autocomplete from the Cody extension settings 4. Site admins can opt out of auto-edits using the `cody-autoedit-experiment-enabled-flag` feature flag The following example demonstrates how to add Fireworks as an allowed LLM provider: ```json "cody.enabled": true, "modelConfiguration": { "sourcegraph": { "modelFilters": { // Only allow "beta" and "stable" models. // Not "experimental" or "deprecated". "statusFilter": ["beta", "stable"], // Allow any models provided by Anthropic, OpenAI, Google and Fireworks. "allow": [ "anthropic::*", // Anthropic models "openai::*", // OpenAI models "google::*", // Google Gemini models "fireworks::*", // Open source models hosted by Sourcegraph ], } } } ```
# Agentic Context Fetching

Learn about agentic context fetching, a mini-agent that uses search and tools to retrieve context.

Cody's agentic context fetching experience can evaluate context and fetch any additional context (MCP, OpenCtx, terminal, etc.) by providing enhanced, context-aware chat capabilities. It extends Cody's functionality by proactively understanding your coding environment and gathering relevant information based on your requests before responding. These features help you get noticeably higher-quality responses. This experience aims to reduce the learning curve associated with traditional coding assistants by minimizing users' need to provide context manually. It achieves this through agentic context retrieval, where the AI autonomously gathers and analyzes context before generating a response. ## Capabilities of agentic chat The agentic context fetching experience leverages several key capabilities, including: - **Proactive context gathering**: Automatically gathers relevant context from your codebase, project structure, and current task - **Agentic context reflection**: Review the gathered context to ensure it is comprehensive and relevant to your query - **Iterative context improvement**: Performs multiple review loops to refine the context and ensure a thorough understanding - **Enhanced response accuracy**: Leverages comprehensive context to provide more accurate and relevant responses, reducing the risk of hallucinations ## What can agentic context fetching do? Agentic context fetching can help you with the following: ### Tool Usage It has access to a suite of tools for retrieving relevant context. These tools include: - **Code Search**: Performs code searches - **Codebase File**: Retrieves the full content from a file in your codebase - **Terminal**: Executes shell commands in your terminal - **Web Browser**: Searches the web for live context - **MCP**: (Configure MCP and add servers)[] to fetch external context - **OpenCtx**: Any OpenCtx providers could be used by the agent It integrates seamlessly with external services, such as web content retrieval and issue tracking systems, using OpenCtx providers. To learn more, [read the OpenCtx docs](/cody/capabilities/openctx). Terminal access is not supported on the Web. It currently only works with VS Code, JetBrains, and Visual Studio editor extensions. ## Terminal access Agentic context fetching can use the CLI Tool to request the execution of shell commands to gather context from your terminal. Its ability to execute terminal commands enhances its context-gathering capabilities. However, it's essential to understand that any information accessible via your terminal could potentially be shared with the LLM. It's recommended not to request information that you don't want to share. Here's what you should consider: - **Requires user consent**: Agentic context fetching will pause and ask for permission each time before executing any shell command. - **Trusted workspaces only**: Commands can only be executed within trusted workspaces with a valid shell - **Potential data sharing**: Any terminal-accessible information may be shared with the LLM Commands are generated by the agent/LLM based on your request. Avoid asking it to execute destructive commands. ## Use cases Agentic context fetching can be helpful to assist you with a wide range of tasks, including: - **Improved response quality**: Helps you get better and more accurate responses than other LLMs, making up for the additional processing time for context gathering a non-issue - **Error resolution**: It can automatically identify error sources and suggest fixes by analyzing error logs - **Better unit tests**: Automatically includes imports and other missing contexts to generate better unit tests ## Enable agentic context fetching Agentic context fetching is enabled by default for all Cody users. It uses LLM reflection and basic tool use steps to gather and refine context before sending it in the final model query. The review step in agentic context fetching experience defaults to Gemini 2.5 Flash and falls back to Claude Haiku or GPT 4.1 mini if Flash is unavailable. You can disable agentic context in your extension settings using `cody.agenticContext`. Terminal access for Enterprise users is disabled by default. To enable it, set the `agentic-chat-cli-tool-experimental` feature flag [terminal access](#terminal-commands). ## MCP support Cody supports [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) for connecting to external context. MCP servers are utilized via Cody’s agentic context fetching. Users can configure multiple local MCP servers via their Cody extension settings. Based on your query, agentic context will determine which MCP tools to invoke and what parameters to provide to the tools. Cody will execute these tools and inject the context from the tool calls into the context window. ### Setting up MCP tools MCP is disabled by default. To enable it, add the `agentic-context-mcp-enabled` feature flag to your Enterprise Sourcegraph instance. Once MCP is enabled, there are two ways to configure an MCP server: #### 1. From the editor extension UI To configure an MCP server from the editor extension UI, click your profile icon in the Cody extension and select the **MCP Settings** option. ![mcp-via-ui](https://storage.googleapis.com/sourcegraph-assets/Docs/mcp-with-ui-2025.png) Create a new server configuration and input the necessary arguments provided by the server. You can disable individual tools in the UI by clicking on them. #### 2. From the editor extension settings - Edit your MCP configuration settings using the `cody.mcpServers` property in your extension settings. - `settings.json` (VSCode) - `cody_settings.json` (JetBrains) - Use the following format when adding a new server In JetBrains, you need the absolute path to the command. Use the `which` command in the terminal to find it. For example, `which npx`. ```json "cody.mcpServers": { "": { "command": "...", "args": [...], "env": { ... }, "disabledTools": [...] } } ``` When you submit a query, Cody will determine whether to use any of the server's tools. ![mcp-via-settings](https://storage.googleapis.com/sourcegraph-assets/Docs/mcp-via-settings.png) ### MCP Best Practices Since MCP is an open protocol and servers can be created by anyone, your mileage may vary. Tool names, descriptions, and the underlying models you have available may all affect the performance of MCP retrieval. To get the best results using your MCP servers, we recommend the following: - Read through the tools of the servers you install and their parameters. Be as explicit as possible in your queries to provide all of the information the tool needs - If you don't get the desired results on the first try, iterate on your query. It may take a couple of tries to get the results you're expecting - Choose high-quality servers. Prefer servers from official sources over third parties. Read through the code to understand how the servers you are installing work ### Limitations - MCP is supported through Cody’s agentic context fetching capabilities. You must have these capabilities enabled in order to utilize MCP servers - Cody currently supports only local MCP servers - MCP offers three main capabilities: Tools, Resources, and Prompts. Currently, Cody supports only Tools - Since most MCP servers require users to create their access tokens, the servers will have the same access as whatever token you provide. For example, if a user has write permissions to Jira and creates an access token with write permissions, they can use Cody to write back into Jira via MCP. It is not possible today to limit write access across all MCP tools broadly. You should use tools with this consideration in mind
# Quickstart for code monitoring ## Introduction In this tutorial, we will create a new code monitor that monitors new appearances of the word "TODO" in your codebase. ## Creating a code monitor Prerequisite: Ensure [email notifications](/admin/observability/alerting#email) are configured in site configuration. 1. On your Sourcegraph instance, click the **Code monitoring** menu item at the top right of your page. Alternatively, go to `https://sourcegraph.example.com/code-monitoring` (where sourcegraph.example.com represents your unique Sourcegraph url). 2. Click the **Create new code monitor** button at the top right of the page. 3. Fill out the **Name** input with: "TODOs". 4. Under the **Trigger** section, click **When there are new search results**. 5. In the **Search query** input, enter the following search query: `TODO type:diff patternType:keyword`. (Note that `type:` and `patternType:` are required as part of the search query for code monitoring.) 1. You can click **Preview results** to see all previous additions or removals of TODO to your codebase. 2. Back in the code monitor form, click **Continue**. 3. Click **Send email notifications** under the **Actions** section. 4. Click **Done**. 5. Click **Create code monitor**. You should now be on `https://sourcegraph.example.com/code-monitoring`, and be able to see the TODO code monitor on the page. ## Sending a test email notification If you want to preview the email notification alerting you of a new result with TODO, follow these steps: 1. In the **Send email notifications** action, click "Send test email". 1. Within a few minutes, you should receive a test email from Sourcegraph with a preview of the email notification. If you want to test receiving an email with a real new result, follow these steps: 1. In any repository that's on your Sourcegraph instance (for purposes of this tutorial, we recommend a dummy or test repo that's not used), add `TODO` to any file. 1. Commit the change, and push it to your code host. 1. Within a few minutes, you should see an email from Sourcegraph with a link to the new result you just pushed. # Code monitoring Supported on [Enterprise](/pricing/enterprise) plans. Currently available via the Web app.

Keep on top of events in your codebase

Watch your code with code monitors and trigger actions to run automatically in response to events. ## Getting started ## Questions & Feedback We want to hear your feedback! [Share your thoughts](mailto:feedback@sourcegraph.com)
# Setting up Webhook notifications Webhook notifications provide a way to execute custom responses to a code monitor notification. They are implemented as a POST request to a URL of your choice. The body of the request is defined by Sourcegraph, and contains all the information available about the cause of the notification. ## Prerequisites - You must not have have the setting `experimentalFeatures.codeMonitoringWebHooks` disabled in your user, org, or global settings. - You must have a service running that can accept the POST request triggered by the webhook notification ## Creating a webhook receiver A webhook receiver is a service that can accept an HTTP POST request with the contents of the webhook notification. The receiver must be reachable from the Sourcegraph cluster using the URL that is configured below. The HTTP POST request sent to the receiver will have a JSON-encoded body with the following fields: - `monitorDescription`: The description of the monitor as configured in the UI - `monitorURL`: A link to the monitor configuration page - `query`: The query that generated `results` - `results`: The list of results that triggered this notification. Contains the following sub-fields - `repository`: The name of the repository the commit belongs to - `commit`: The commit hash for the matched commit. - `diff`: The matching diff in unified diff format. Only set if the result is a diff match. - `matchedDiffRanges`: The character ranges of `diff` that matched `query`. Only set if the result is a diff match. - `message`: The matching commit message. Only set if the result is a commit match. - `matchedMessageRanges`: The character ranges of `message` that matched `query`. Only set if the result is a commit match. Example payload: ```json { "monitorDescription": "My test monitor", "monitorURL": "https://sourcegraph.com/code-monitoring/Q29kZU1vbml0b3I6NDI=?utm_source=", "query": "repo:camdentest -file:id_rsa.pub BEGIN", "results": [ { "repository": "github.com/test/test", "commit": "7815187511872asbasdfgasd", "diff": "file1.go file2.go\n@ -97,5 +97,5 @ func Test() {\n leading context\n+matched added\n-matched removed\n trailing context\n", "matchedDiffRanges": [ [ 66, 73 ], [ 91, 98 ] ] }, { "repository": "github.com/test/test", "commit": "7815187511872asbasdfgasd", "message": "summary line\n\nsample\ncommit\nmessage\n", "matchedMessageRanges": [ [ 15, 19 ] ] } ] } ``` ## Configuring a code monitor to send Webhook notifications 1. In Sourcegraph, click on the "Code Monitoring" nav item at the top of the page. 1. Create a new code monitor or edit an existing monitor by clicking on the "Edit" button next to it. 1. Go through the standard configuration steps for a code monitor and select action "Call a webhook". 1. Paste your webhook URL into the "Webhook URL" field. 1. Click on the "Continue" button, and then the "Save" button. # Start monitoring your code This page lists code monitors that are commonly used and can be used across most codebases. ## Watch for consumers of deprecated endpoints ``` f:\.tsx?$ patterntype:regexp fetch\(['"`]/deprecated-endpoint ``` If you’re deprecating an API or an endpoint, you may find it useful to set up a code monitor watching for new consumers. As an example, the above query will surface fetch() calls to `/deprecated-endpoint` within TypeScript files. Replace `/deprecated-endpoint` with the actual path of the endpoint being deprecated. ## Get notified when a file changes ``` patterntype:regexp repo:^github\.com/sourcegraph/sourcegraph$ file:SourcegraphWebApp\.tsx$ type:diff ``` You may want to get notified when a given file is changed, regardless of the diff contents of the change: the above query will return all changes to the `SourcegraphWebApp.tsx` file on the `github.com/sourcegraph/sourcegraph` repo. ## Get notified when a specific function call is added ``` repo:^github\.com/sourcegraph/sourcegraph$ type:diff select:commit.diff.added Sprintf ``` You may want to monitor new additions of a specific function call, for example a deprecated function or a function that introduces a security concern. This query will notify you whenever a new addition of `Sprintf` is added to the `sourcegraph/sourcegraph` repository. This query selects all diff additions marked as "+". If a call of `Sprintf` is both added and removed from a file, this query will still notify due to the addition. # Slack notifications for code monitors You can set up [code monitors](/code_monitoring) to send notifications about new matching search results to Slack channels. ## Requirements - You must have permission to create apps in your organization's Slack workspace. ## Usage 1. In Sourcegraph, click on the "Code Monitoring" nav item at the top of the page. 1. Create a new code monitor or edit an existing monitor by clicking on the "Edit" button next to it. 1. Go through the standard steps for a code monitor (if it's a new one) and select the action **Send Slack message to channel**. 1. Paste your webhook URL into the "Webhook URL" field. (See "[Creating a Slack incoming webhook URL](#creating-a-slack-incoming-webhook-url)" below for detailed instructions.) 1. Click on the "Continue" button, and then the "Save" button. ### Creating a Slack incoming webhook URL 1. Go to https://api.slack.com/apps and sign in to your Slack account if necessary. 1. Click on the "Create an app" button. 1. Create your app "From scratch". 1. Give your app a name and select the workplace you want notifications sent to. # How-tos * [Starting points](/code_monitoring/how-tos/starting_points) * [Setting up Slack notifications](/code_monitoring/how-tos/slack) * [Setting up Webhook notifications](/code_monitoring/how-tos/webhook) # Explanations * [Core concepts](/code_monitoring/explanations/core_concepts) * [Best practices](/code_monitoring/explanations/best_practices) # Core concepts Code monitors allow you to keep track of and get notified about changes in your code. Some use cases for code monitors include getting notifications for potential secrets, anti-patterns, or common typos committed to your codebase. Code monitors are made up of two main elements: **Triggers** and **Actions**. ## Triggers A _trigger_ is an event which causes execution of an action. Currently, code monitoring supports one kind of trigger: "When new search results are detected" for a particular search query. When creating a code monitor, users will be asked to specify a query as part of the trigger. Sourcegraph will run the search query over every new commit for the searched repositories, and when new results for the query are detected, a trigger event is emitted. In response to the trigger event, any _actions_ attached to the code monitor will be executed. **Query requirements** A query used in a "When new search results are detected" trigger must be a diff or commit search. In other words, the query must contain `type:commit` or `type:diff`. This allows Sourcegraph to detect new search results periodically. ## Actions An _action_ is executed in response to a trigger event. Currently, code monitoring supports three different actions: * Sending a notification email to the owner of the code monitor * Sending a Slack message to a preconfigured channel (Beta) * Sending a webhook event to an endpoint of your choosing (Beta) ## Current flow To put it all together, a code monitor has a flow similar to the following: A user creates a code monitor, which consists of: * a name for the monitor * a trigger, which consists of a search query to run periodically, * and an action, which is sending an email, sending a Slack message, or sending a webhook event Sourcegraph runs the query periodically over new commits. When new results are detected, a notification will be sent with the configured action. It will either contain a link to the search that provided new results, or if the "Include results" setting is enabled, it will include the result contents. # Best practices There are some best practices we recommend when creating code monitors. ## Privacy and visibility ### Do not include confidential information in monitor names Every code monitor has a name that will be shown wherever the monitor is referenced. In notification actions this name is likely to be the only information about the event, so it’s important for identifying what was triggered, but also has to be “safe” to expose in plain text emails. ### Do not include results when the notification destination is untrusted Each code monitor action has the ability to include the result contents when sending a notification. This is often convenient because it lets you immediately see which results triggered the notification. However, because the result contents include the code that matched the search query, they may contain sensitive information. Care should be taken to only send result contents if the destination is secure. For example, if sending the results to a Slack channel, every user that can view that channel will also be able to view the notification messages. The channel should be properly restricted to users who should be able to view that code. ## Scale Code monitors have been designed to be performant even for large Sourcegraph instances. There are no hard limits on the number of monitors or the volume of code monitored. However, depending on a number of factors such as the number of code monitors, the number of repos monitored, the frequency of commits, and the resources allocated to your instance, it's still possible to hit soft limits. If this happens, your code monitor will continue to work reliably, but it may execute more infrequently. # Quickstart for Code Insights Get started and create your first [code insight](/code_insights/) in 5 minutes or less. ## Introduction In this guide, you'll create a Sourcegraph code insight that tracks the number of `TODOs` that appear in parts of your codebase. For more information about Code Insights see the [Code Insights](/code_insights/) documentation. ## Requirements - You are a Sourcegraph enterprise customer. (Want code insights but aren't enterprise? [Let us know](mailto:feedback@sourcegraph.com).) - Your Sourcegraph instance has at least 1 repository. (See "[Quickstart](/#quick-install)" on how to setup a Sourcegraph instance.) - Your Sourcegraph instance is deployed via [Docker Compose](/admin/deploy/docker-compose/) or [Kubernetes](/admin/deploy/kubernetes/). - You are running Sourcegraph version 3.31.1 (August 2021 release) or later. - Note: If you're on Sourcegraph version 3.24 to 3.28, you can instead follow [this gist](https://gist.github.com/Joelkw/f0582b164578aabc3ac936dee43f23e0) to create an insight. Due to the early stage of the product, it's more likely you'll run into trouble, though, so we recommend that you either upgrade your Sourcegraph or reach out to your Sourcegraph reps for help. ## Enable Code Insights ### 1. [If need be] Enable the experimental feature flag **If you're on Sourcegraph version 3.31 or earlier** and you don't already see the "Insights" item in your main nav bar, you'll need to add the following to either your Sourcegraph user settings `sourcegraph.example.com/users/[username]/settings` or organization settings `sourcegraph.example.com/organizations/[your_org]/settings` (and _not_ in your site configuration settings): ```json "experimentalFeatures": { "codeInsights": true }, "extensions": { "sourcegraph/code-stats-insights": true, "sourcegraph/search-insights": true } ``` If you put this in your organization settings, everyone on your Sourcegraph insights will be able to see the "Insights" navbar menu item and create their own code insights. If you put the flag in your user settings, only you will have those abilities. (Enabling code insights organization-wide doesn't mean that other users can automatically see the code insights you create, however – you can control that visibility per individual insight.) ### 2. Visit your example.sourcegraph.com/insights page and select "+ Create insight" ### 3. On the insight type selection page, select "Track changes" This creates a code insight tracking an arbitrary input that you could run a Sourcegraph search for. Your other options are to: * [create a "Detect and track" insight](/code_insights/explanations/automatically_generated_data_series) to automatically generate your data series according to capture group matching (3.35+). * [create a language-based insight](/code_insights/language_insight_quickstart) to show you language breakdown in your repositories. ### 4. Select the repositories you want to search If you want an insight running over a specific amount of repositories, enter the repositories in the repository URL format, like `github.com/sourcegraph/sourcegraph`. Separate multiple repositories with a comma. The form field will validate that you've entered the repository correctly. If you want to run an insight over all repositories, instead check the box to do so (available in Sourcegraph 3.31.1 and later). If you otherwise want to exclude specific repositories, you can do so after creating the insight by using filters (step 10). From Sourcegraph 4.5 you are able to define the repositories you want your insight to run over using a Sourcegraph search query. [Read the reference to learn more](/code_insights/references/repository_scope). ### 5. Define a data series to track the incidence of `TODO` A data series becomes a line on your graph. You can **Name** this data series something like `TODOs count`. To track the incidence of TODOs, you can set your **Search query** to be simply `TODO`. You can also select the color of your data series. ### 6. Add a title to the insight Enter a descriptive **Title** for the chart, like `Count of TODOs in [repository name]`. ### 7. Set the distance between data points Code insights give you twelve datapoints for each data series on insight creation. Setting it to one month means you'll see the results over the last year. ### 8. Click "create code insight" and view your insight. You'll be taken to the `example.sourcegraph.com/insights` page and can view your insight. ### 9. Filter your insight to explore it further Click the filter button in the top right of an insight card to open the filters panel. This allows you to filter the insight down to a subset of repositories through inclusion or exclusion using regular expressions. For more details, see [How to filter an insight](/code_insights/how-tos/filtering_an_insight). # Quickstart for Language Code Insights Get started and create your first [language code insight](/code_insights/) in 5 minutes or less. ## Introduction > This quickstart guide assumes that **you have already completed [step 1, enabling the feature flag](/code_insights/quickstart#1-enable-the-experimental-feature-flag)**, on the main code insights quickstart guide. In this guide, you'll create a Sourcegraph language code insight that shows the percentage of lines of code in a repository by language. For more information about Code Insights see the [Code Insights](/code_insights/) documentation. ### 1. Visit your Sourcegraph instance /Insights page and select "+ Create new insight" If you don't see the /Insights page you need to [enable code insights](quickstart#1-enable-the-experimental-feature-flag). ### 2. On the insight type selection page, select "Create language usage insight" This creates a code insight tracking how many lines of code you have in each language. If you are more interested tracking the historical or future result count of an arbitrary Sourcegraph search, [follow this tutorial](/code_insights/quickstart) instead. ### 3. Once on the "Set up new language usage insight" form fields page, enter the repository you want to analyze. Enter repositories in the repository URL format, like `github.com/sourcegraph/docs`. We recommend a small repository so that you get a quick result. The form field will validate that you've entered the repository correctly. In the current version, you can only set up a language insight for one repository at a time. (Want us to prioritize the ability to analyze multiple repositories in one pie chart? [Please do let us know](mailto:feedback@sourcegraph.com).) ### 4. Title your insight Title your language insight, generally something like the repository name or `Language usage in RepositoryName`. ### 5. Set the 'Other' category threshold This determines below what % the insight groups all other languages into an 'Other' languages category. Example: if the Threshold is set to 3%, and 2% of your code is in python and 2% of your code is in ruby, then both will be grouped into 'Other' and the value for 'Other' will be 4%. ### 6. Click "create code insight" to view and save your insight You'll be taken to the sourcegraph.example.com/insights page and can view your insight. # Code Insights Supported on [Enterprise](/pricing/enterprise) plans. Currently available via the Web app.

Anything you can search, you can track and analyze

Code Insights reveals high-level information about your codebase, based on both how your code changes over time and its current state. Code Insights is based on our universal code search, making it precise and configurable. Track anything that can be expressed with a Sourcegraph search query: migrations, package use, version adoption, code smells, codebase size and much more, across 1,000s of repositories. Code Insights will backfill years of historical data from your version control, so you can immediately reveal current trends and baselines.
# Search results aggregations use cases and recipes Here are some common use cases for the [in-search-results aggregations](/code_insights/explanations/search_results_aggregations) powered by code insights. ## Popular ### What versions of Go are we using most? See all the go versions in your codebase (group by capture group) ```sgquery file:go\.mod$ /go\s*(\d\.\d+)/ ``` ### What are the open source licenses most used in our codebase? See all the licenses included, by frequency (group by capture group) ```sgquery file:package.json /"license":\s(.*),/ ``` ### Which repositories use a specific internal library the most? See which repositories import a library (group by repository) ```sguqery from '@sourcegraph/wildcard' ``` ### Which directories have the longest eslint ignore file? See which files have the most linter override rules within a repository (group by file) ```sgquery file:^\.eslintignore /.\n/ repo:^github\.com/sourcegraph/sourcegraph$ ``` ### Who knows most about a library or component? See who has added the most uses of a component to a specific repository in the last three months ```sgquery nodeComponent type:diff select:commit.diff.added repo:sourcegraph/sourcegraph$ after:"3 months ago" ``` ## By capture group ### What versions of log4j exist in our codebase? See all the different subversions of log4j present in your code ```sgquery lang:gradle /org\.apache\.logging\.log4j['"].*?(2\.\d+\.\d+)/ ``` ### What breaks most commonly? See what topics most frequently appear in "fix [x]" commit messsages ```sgquery type:commit repo:^github\.com/sourcegraph/sourcegraph$ after:"5 days ago" /Fix (\\w+)/ ``` ### What are the most common email addresses we direct users to? See every email address hardcoded, by frequency ```sgquery /(\w+)\@sourcegraph\.com/ ``` ### How can we see all our different tracer calls to remove unnecessary ones or encourage proper usage? See all your tracer calls to track the growth of, or minimize spend on, tools like Datadog. ```sgquery /tracer\.trace\(([\s"'\w@\/:^.#,+-=]+)\)/ ``` ## By repository ### Which repositories use a specific internal library the most? See which repositories import a library ```sguqery /from\s'\@sourcegraph\/wildcard/ ``` ### Which teams (repositories) have the most usages of a vulnerable function or library? ```sgquery vulnerableFunc( ``` ### Which repositories have the longest top-level eslint ignore files? See which repositories are using the most linter overrides ```sgquery file:^\.eslintignore /.\n/ ``` ## By file ### Which files should we migrate first? See which files have the most usage of a library you want to deprecate, such as the log15 library ```sgquery repo:^github\.com/sourcegraph/sourcegraph$ /log15\.(?:Debug|Info|Warn|Error)/ ``` ### Which are our biggest package.json files? See which repositories have the most scripts or dependencies ```sgquery file:package\.json , ``` ### Which directories have the longest eslint ignore file? See which files have the most linter override rules within a repository ```sgquery file:^\.eslintignore /.\n/ repo:^github\.com/sourcegraph/sourcegraph$ ``` ## By author ### Who knows most about a library or component? See who has added the most uses of a component to a specific repository in the last three months ```sgquery nodeComponent type:diff select:commit.diff.added repo:sourcegraph/sourcegraph$ after:"3 months ago" ``` ### Who worked on a recent migration? See who most often had commits mentioning what you migrated away from (for example: migrating off bootstrap) ```sgquery bootstrap type:commit r:sourcegraph/sourcegraph$ after:"3 months ago" ``` # Requirements Code Insights has requirements for the Sourcegraph server version, its deployment type, and its connected code hosts. ## Sourcegraph server While the latest version of Sourcegraph server is always recommended: - Version 3.28 is the minimum version required to use Code Insights over individual repositories - Version 3.31.1 is the minimum version required to use Code Insights over all repositories ## Sourcegraph deployment You can only use Code Insights on a [Docker Compose](/admin/deploy/docker-compose/) or [Kubernetes](/admin/deploy/kubernetes/) Sourcegraph deployment. ## Code hosts Sourcegraph Code Insights is compatible with any [Sourcegraph-compatible code host](/admin/repo/), except: * Perforce repositories making use of sub-repo permissions are not supported # Code Insights repository scope A Code Insight runs on a list of repositories as specified on insight creation. The scope options are: * Manually specify a list of repositories * Run the insight over all repositories (Sourcegraph 3.31.1+). * From Sourcegraph 4.5 this is achieved through a `repo:.*` search query. * Run the insight over repositories as returned from a repository search query (Sourcegraph 4.5+) * This replaces the "Run your insight over all repositories" checkbox in prior versions ## Using the repository search query box ![Code Insights repository search box with a query for repo:sourcegraph](https://storage.googleapis.com/sourcegraph-assets/docs/images/code_insights/create_insight_repo_selection.png) The repository search query box allows you to search for repositories on your Sourcegraph instance using standard Sourcegraph `repo` filters, as well as boolean operators. Some example use cases might be: * I want my insight to run over all the repositories in the `sourcegraph` organisation, but not the `handbook` repository ```sgquery repo:sourcegraph/* -repo:sourcegraph/handbook$ ``` * I want my insight to run over all my repositories that have `github-actions` ```sgquery repo:has.file(github-actions) ``` The repository search box functions as the Sourcegraph search box, so it will suggest repository names as you type. ### Refining and previewing your repositories After writing your repository search query, the Code Insights creation UI will display how many repositories the query has resolved. If this number is unexpected, you can preview the repositories using the `Preview results` link. > NOTE: Repositories will include archived repositories and forks by default. ## How is the list of repositories resolved? If you are using a search query to define the list of repositories to run your insight over, then: 1. On insight creation the list of repositories that matches your search query is fetched. All the historical searches that are used to backfill your insight are ran against this unchanged list of repositories. 2. Every newer point added to your insight will then fetch results using (your_repo_search_query) (your_insight_search_query), so the repositories will be resolved against the global state of your instance. * For example, if you have a repo query for `repo:docs or repo:handbook` and a query for `lang:Markdown TODO`, every point will fetch results for the following query: ```sgquery (repo:docs or repo:handbook) (lang:Markdown TODO) ``` # Licensing and Limited Access ## License Instances need a license key with the `code-insights` tag to have full access to Code Insights features, or they need to be on the new 2022 "Business" or "Enterprise" plans. ## Limited Access If an instance does not have full access via the correct license key, then at most, an Enterprise plan instance can have a maximum of two global code insights for "free" without having a license. The in-search aggregations feature that appears on the search UI is available to all users on any license, but future features, like saving those aggregations to dashboards, would require an insights-including license. # References The following is a list of reference documents for Code Insights: - [Common use cases and recipes](/code_insights/references/common_use_cases) - [Common reasons code insights may not match search results](/code_insights/references/common_reasons_code_insights_may_not_match_search_results) - [Incomplete data points](/code_insights/references/incomplete_data_points) - [Licensing and limited access](/code_insights/references/license) - [Managing code insights with the API](/api/graphql/managing-code-insights-with-api) - [Requirements](/code_insights/references/requirements) - [Code Insight repository scope](/code_insights/references/repository_scope) # Incomplete data points There are a few cases when a Code Insight may return incomplete data. In all of these cases, if data is returned at all, it will be an undercount. See the below situations for tips on avoiding and troubleshooting these errors. ## Search Queries This guide applies to all types of Code Insights that use search queries. Language Stats Insights do not use search queries. ## Identify affected repositories Through the Code Insights you can only see an aggregated warning for incomplete datapoints. If you're running the Code Insight with more than one repository, you may want to identify which repositories are causing incomplete datapoints. To identify affected repositories you first need the series ID. Go to your insights dashboard, and click on the hamburger menu in the top right of your insight's widget. Click on "Get shareable link". It may look like the one below. The last part is the ID. ``` https://test.sourcegraph.com/insights/aW5zaWdodF92aWV3OiIyZ1ZmQzMxMjEwRGhKVkxvc1YybnBqbEduYnUi ``` With the ID, you can now use the GraphQL API Console through your user settings page, or navigate to `/api/console` on your Sourcegraph instance. Paste the GraphQL query below into the query field, and replace `MY_SERIES_ID` with the ID you copied from above. ``` { insightViews(id: "MY_SERIES_ID") { nodes { repositoryDefinition { ...on RepositorySearchScope { search } } dataSeries { status { incompleteDatapoints { time ...on TimeoutDatapointAlert { __typename repositories { name } } ...on GenericIncompleteDatapointAlert { __typename repositories { name } } } } } } } } ``` Running this query will give you a list of incomplete datapoints along with the repositories that are causing them. You can then follow the other recommendations on this page to address issues for those repositories. ## Timeout errors For searches that take a long time to complete, it's possible for them to timeout before the search ends, and before we can record the data value. To address this, try to minimize or avoid cases where your insight data series: 1. Runs over extra large sets of repositories (scope your insight further to fewer repositories) 1. Uses many boolean combinations (consider splitting into multiple data series) 1. Runs a complex search over an especially large monorepo (consider optimizing your search query to be more specific or include more filters, like `lang:` or `file:`) In addition: 1. Timeout errors on points that have been backfilled before the creation date of the insight are more likely to occur on single, large repositories, because backfill points are calculated by running many searches, repository by repository, individually. 1. Timeout errors on points that have been snapshot after the creation date of the insight are more likely to occur on insights running complex searches over large numbers of repositories, because snapshot points are calculated by running a single global search against the current index. You can read more about this case in our [limitations](/code_insights/explanations/current_limitations_of_code_insights#accuracy-considerations-for-an-insight-query-returning-a-large-result-set). If the data is available, the error alert will inform you which times the search has timed out. ## Strategies for very large repositories When dealing with large repositories, several strategies can help identify and manage search limitations effectively. The goal is to find a query that can execute successfully, and then ramp up complexity until we find the breaking point. ### Use Code Search to test your query You can use Code Search to verify if the query is able to complete within the given timeout. Once a query was executed the results may be indexed and you need to choose a different commit or date to achieve comparable results to Code Insights. #### Unlimited results Code Search limits the number of results by default. Code Insights however needs to count through all the results. Add `count:all` to your query to get comparable results. #### Picking the right commit When Code Insights runs a search query, it will do so for 12 commits spread out over the configured time range. This unindexed search can take longer the further back in time the search goes. To test that the slowest search will succeed in time, we recommend using the `rev:at.time(...)` (available from version 5.4.0) with the time range that you selected. E.g. if your Code Insight looks at the last 2 years you should use `rev:at.time(2y)`. For example the Code Insights query `file:.*\.md hello repo:^github\.com/my_org/my_repo count:all` should be written as `context:global file:.*\.md hello repo:^github\.com/my_org/my_repo count:all rev:at.time(2y)` in Code Search. If your Sourcegraph instance is on a version older than 5.4.0, you can pick a commit sha from e.g. 2 years ago. Here the query `file:.*\.md hello repo:^github\.com/my_org/my_repo count:all` becomes `context:global file:.*\.md hello repo:^github\.com/my_org/my_repo@my_commit_sha count:all`. #### Timeout If the query fails with a timeout before one minute has passed, add the parameter `timeout:1m`. ### Precise queries compute faster If your query is not able to compute results in a reasonable time, you can try to reduce the number of results returned by the query. For example, if you want to track the version of a NPM dependency in your code base, searching for `my_library file:package.json` will compute much faster because there are less files to look at and fewer results to return. We recommend to make your query as precise as possible and reduce the number of results until you reach a query that is able to compute fast enough. Here are some tips: - Search only files with a particular ending. E.g. use `file:.*\.md` to search for files with the `.md` ending. - Search for files with a certain language. E.g. use `lang:javascript` to search for all JavaScript files. Please note that this can be slower than the file ending filter, but may be necessary for ambiguous file endings (e.g. `.m` is used for Objective-C and Matlab). - Put quotes around literal terms, unless you want to search for multiple keywords. E.g. searching for `"hello world"` will only yield results where both words are connected by a whitespace, but `hello world` will yield results where either word appears. To learn more about writing precise queries, see our [search query syntax](/code-search/queries). ### Increase the timeout By default, search queries have a one minute timeout. This value is capped by the setting `search.limits.maxTimeoutSeconds` which by default is also one minute. Your site admin can change this value to increase the maximum timeout. Once it is increased, you can use the search parameter `timeout:` to give the query more time to run. Example: ``` timeout:10m ``` Note that very long searches can have a negative impact on performance, so it's important to use this parameter with caution. ## Other errors For other errors, please reach out to our support team through your usual channels or at support@sourcegraph.com. # Common Code Insights use cases and recipes Here are some common use cases for Code Insights and example data series queries you could use. For all use cases, you can also explore your insight by [filtering repositories in real time](/code_insights/how-tos/filtering_an_insight) or add any [Sourcegraph search filter](/code-search/queries/language#search-pattern) to the data series query to filter by language, directory, or content. Currently, the sample queries using commit and diff searches are only supported for insights running over explicit lists of specific repositories. *The sample queries below make the assumption you [do not want to search fork or archived](/code_insights/references/common_reasons_code_insights_may_not_match_search_results#not-including-fork-no-and-archived-no-in-your-insight-query) repositories. You can include those flags if you do.* ## Popular ### Terraform versions Detect and track which Terraform versions are present or most popular in your codebase ```sgquery app.terraform.io/(.*)\n version =(.*)1.1.0 patternType:regexp lang:Terraform ``` ```sgquery app.terraform.io/(.*)\n version =(.*)1.2.0 patternType:regexp lang:Terraform ``` ### Global CSS to CSS modules Tracking migration from global CSS to CSS modules ```sgquery select:file lang:SCSS -file:module patterntype:regexp ``` ```sgquery select:file lang:SCSS file:module patterntype:regexp ``` ### Vulnerable and fixed Log4j versions Confirm that vulnerable versions of log4j are removed and only fixed versions appear ```sgquery lang:gradle org\.apache\.logging\.log4j['"] 2\.(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16)(\.[0-9]+) patterntype:regexp ``` ```sgquery lang:gradle org\.apache\.logging\.log4j['"] 2\.(17)(\.[0-9]+) patterntype:regexp ``` ### Yarn adoption Are more repos increasingly using yarn? Track yarn adoption across teams and groups in your organization ```sgquery select:repo file:yarn.lock ``` ### Java versions Detect and track which Java versions are most popular in your codebase *Uses the [detect and track](/code_insights/explanations/automatically_generated_data_series) capture groups insight type* ```sgquery file:pom\.xml$ (.*) ``` ### Linter override rules A code health indicator for how many linter override rules exist ```sgquery file:^\.eslintignore .\n patternType:regexp ``` ### Language use over time Track the growth of certain languages by file count ```sgquery select:file lang:TypeScript ``` ```sgquery select:file lang:JavaScript ``` ### Pinned vs Unpinned Docker Base Images Track how many unpinned images exist relative to pinned images ```sgquery ^FROM (\w+\/)?\w+:latest($|\s) file:Dockerfile patternType:regexp ``` ```sgquery ^FROM (\w+\/)?\w+:latest@sha file:Dockerfile patternType:regexp ``` ## Migration ### Config or docs file How many repos contain a config or docs file in a specific directory ```sgquery select:repo file:docs/*/new_config_filename ``` ### “blacklist/whitelist” to “denylist/allowlist” How the switch from files containing “blacklist/whitelist” to “denylist/allowlist” is progressing ```sgquery select:file blacklist OR whitelist ``` ```sgquery select:file denylist OR allowlist ``` ### Global CSS to CSS modules Tracking migration from global CSS to CSS modules ```sgquery select:file lang:SCSS -file:module patterntype:regexp ``` ```sgquery select:file lang:SCSS file:module patterntype:regexp ``` ### Python 2 to Python 3 How far along is the Python major version migration ```sgquery #!/usr/bin/env python3 ``` ```sgquery #!/usr/bin/env python2 ``` ### React Class to Function Components Migration What's the status of migrating to React function components from class components ```sgquery patternType:regexp const\s\w+:\s(React\.)?FunctionComponent ``` ```sgquery patternType:regexp extends\s(React\.)?(Pure)?Component ``` ## Adoption ### New API usage How many repos or teams are using a new API your team built ```sgquery select:repo ourApiLibraryName.load ``` ### Yarn adoption Are more repos increasingly using yarn? Track yarn adoption across teams and groups in your organization ```sgquery select:repo file:yarn.lock ``` ### Frequently used databases Which databases we are calling or writing to most often ```sgquery redis\.set patternType:regexp ``` ```sgquery graphql\( patternType:regexp ``` ### Large or expensive package usage Understand if a growing number of repos import a large/expensive package ```sgquery select:repo import\slargePkg patternType:regexp ``` ### React Component use How many places are importing components from a library ```sgquery from '@sourceLibrary/component' patternType:keyword ``` ### CI tooling adoption How many repos are using our CI system ```sgquery file:\.circleci/config.yml select:repo ``` ## Deprecation ### CSS class The removal of all deprecated CSS class ```sgquery deprecated-class ``` ### Icon or image The removal of all deprecated icon or image instances ```sgquery 2018logo.png ``` ### Structural code pattern Deprecating a structural code pattern in favor of a safer pattern, like how many tries don't have catches ```sgquery try {:[_]} catch (:[e]) { } finally {:[_]} lang:java patternType:structural ``` ### Tooling The progress of deprecating tooling you’re moving off of ```sgquery deprecatedEventLogger.log ``` ### Var keywords Number of var keywords in the code base (ES5 deprecation) ```sgquery (lang:TypeScript OR lang:JavaScript) var ... = patterntype:structural ``` ### Consolidation of Testing Libraries Which React test libraries are being consolidated ```sgquery from '@testing-library/react' ``` ```sgquery from 'enzyme' ``` ## Versions and patterns These examples are all for use with the [automatically generated data series](/code_insights/explanations/automatically_generated_data_series) of "Detect and track" Code Insights, using regular expression capture groups. ### Java versions Detect and track which Java versions are most popular in your codebase ```sgquery file:pom\.xml$ (.*) ``` ### License types in the codebase See the breakdown of licenses from package.json files ```sgquery file:package.json "license":\s"(.*)" ``` ### All log4j versions Which log4j versions are present, including vulnerable versions ```sgquery lang:gradle org\.apache\.logging\.log4j['"] 2\.([0-9]+)\. ``` ### Python versions Which python versions are in use or haven’t been updated ```sgquery #!/usr/bin/env python([0-9]\.[0-9]+) ``` ### Node.js versions Which node.js versions are present based on nvm files ```sgquery nvm\suse\s([0-9]+\.[0-9]+) ``` ### CSS Colors What CSS colors are present or most popular ```sgquery color:#([0-9a-fA-f]{3,6}) ``` ### Types of checkov skips See the most common reasons for why secuirty checks in checkov are skipped ```sgquery patterntype:regexp file:.tf #checkov:skip=(.*) ``` ### Tracer calls See all your tracer calls to minimize spend on, or track the growth of, tools like Datadog. ```sgquery tracer\.trace\(([\s"'\w@\/:^.#,+-=]+)\) ``` ## Code health ### TODOs How many TODOs are in a specific part of the codebase (or all of it) ```sgquery TODO ``` ### Linter override rules A code health indicator for how many linter override rules exist ```sgquery file:^\.eslintignore .\n patternType:regexp ``` ### Commits with “revert” How frequently there are commits with “revert” in the commit message ```sgquery type:commit revert ``` ### Deprecated calls How many times deprecated calls are used ```sgquery lang:java @deprecated ``` ### Storybook tests How many tests for Storybook exist ```sgquery patternType:regexp f:\.story\.tsx$ \badd\( ``` ### Repos with Documentation How many repos do or don't have READMEs ```sgquery repohasfile:readme select:repo ``` ```sgquery -repohasfile:readme select:repo ``` ### Ownership via CODEOWNERS files How many repos do or don't have CODEOWNERS files ```sgquery repohasfile:CODEOWNERS select:repo ``` ```sgquery -repohasfile:CODEOWNERS select:repo ``` ### CI tooling adoption How many repos are using our CI system ```sgquery file:\.circleci/config.yml select:repo ``` ## Security ### Vulnerable open source library Confirm that a vulnerable open source library has been fully removed, or see the speed of the deprecation ```sgquery vulnerableLibrary@14.3.9 ``` ### API keys How quickly we notice and remove API keys when they are committed ```sgquery regexMatchingAPIKey patternType:regexp ``` ### Vulnerable and fixed Log4j versions Confirm that vulnerable versions of log4j are removed and only fixed versions appear ```sgquery lang:gradle org\.apache\.logging\.log4j['"] 2\.(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16)(\.[0-9]+) patterntype:regexp ``` ```sgquery lang:gradle org\.apache\.logging\.log4j['"] 2\.(17)(\.[0-9]+) patterntype:regexp ``` ### How many tests are skipped See how many tests have skip conditions ```sgquery (this.skip() OR it.skip) lang:TypeScript ``` ### Tests amount and types See what types of tests are most common and total counts ```sgquery patternType:regexp case:yes \b(it|test)\( f:/end-to-end/.*\.test\.ts$ ``` ```sgquery patternType:regexp case:yes \b(it|test)\( f:/regression/.*\.test\.ts$ ``` ```sgquery patternType:regexp case:yes \b(it|test)\( f:/integration/.*\.test\.ts$ ``` ### Types of checkov skips See the most common reasons for why secuirty checks in checkov are skipped *Uses the [detect and track](/code_insights/explanations/automatically_generated_data_series) capture groups insight type* ```sgquery patterntype:regexp file:.tf #checkov:skip=(.*) ``` ## Other ### Typescript vs. Go Are there more Typescript or more Go files ```sgquery select:file lang:TypeScript ``` ```sgquery select:file lang:Go ``` ### iOS app screens What number of iOS app screens are in the entire app ```sgquery struct\s(.*):\sview$ patternType:regexp lang:swift ``` ### Adopting new API by Team Which teams or repos have adopted a new API so far ```sgquery file:mobileTeam newAPI.call ``` ```sgquery file:webappTeam newAPI.call ``` *Or [filter teams by repositories](/code_insights/how-tos/filtering_an_insight) in real time* ### Problematic API by Team Which teams have the most usage of a problematic API ```sgquery problemAPI file:teamOneDirectory ``` ```sgquery problemAPI file:teamTwoDirectory ``` *Or [filter teams by repositories](/code_insights/how-tos/filtering_an_insight) in real time* ### Data fetching from GraphQL What GraphQL operations are being called often ```sgquery patternType:regexp requestGraphQL(\(|<[^>]*>\() ``` ```sgquery patternType:regexp (query|mutate)GraphQL(\(|<[^>]*>\() ``` ```sgquery patternType:regexp use(Query|Mutation|Connection|LazyQuery)(\(|<[^>]*>\() ``` # Common reasons code insights may not match search results There are a few reasons why chart data series' most recent datapoint may show you a different number of match counts than the same search query run in Sourcegraph manually. ## If the chart data point shows *higher* counts than a manual search ### [For versions pre-3.40] Not including `fork:no` and `archived:no` in your insight query Because code insights historical search defaults to `fork:yes` and `archived:yes`, but a Sourcegraph search via the web interface or CLI does not, it may be that your insight data series is including results from repositories that are excluded from a Sourcegraph search. Try running the same search again manually with `fork:yes` and `archived:yes` filters. > NOTE: 3.40+ version defaults to `fork:no` and `archived:no`, the same way the search UI does. ### Manual search will not include unindexed repositories All repositories in a historical search are unindexed, but a manual Sourcegraph search only includes indexed repositories. It's possible your manual searches are missing results from unindexed repositories. To investigate this, one can compare the list of repositories in the manual search (use a `select:repository` filter) with the list of repositories in the insight `series_points` database table. To see why a repository may not be indexing, refer to [this guide](/admin/troubleshooting#sourcegraph-is-not-returning-results-from-a-repository-unless-repo-is-included). ## If the chart data point shows *lower* counts than a manual search ### New matches created since the insight datapoint ran Currently, a data series' most recent datapoint defaults to the end of the prior month. It's possible that in the time between when your insight ran and when you ran a manual search, new matches have been added to your codebase. To confirm this, you can run `type:diff` or `type:commit` searches using the `after:` filter, but note that those filters only support up to 10,000 repositories, so you may first need to limit your search repository set. ### Repository timeouts caused a datapoint to miss results If your code insight is very large, it is possible that a few (\<1% in 100+ manual tests over 26,000 repositories) repositories failed to return match counts due to timing out while searching. To check this, you can run the following GraphQL query in the Sourcegraph GraphQL API: ```graphql query debug { insightViews(id: "INSIGHT_ID") { nodes { dataSeries { label status { pendingJobs completedJobs failedJobs } } } } insightViewDebug(id: "INSIGHT_ID") { raw } } ``` where `INSIGHT_ID` can be found in the "edit" page for the insight (selectable from the three-dot dropdown on the insight) after `...edit/`. It will look like `https://yourdomain.sourcegraph.com/insights/edit/INSIGHT_ID?dashboardId=all`. The `INSIGHT_ID` can also be found in the url of the single insight view found by clicking on the title of the insight. The ID will be in the url, for example, `https://sourcegraph.yourdomain.com/insights/insight/{INSIGHT_ID}` If there are `failedJobs`, there may be timeouts or similar issues affecting your insight. `insightViewDebug` was added in 4.2 to give you more raw information on your insight. # How-tos The following is a list of how-tos that show how to use Code Insights. - [Creating a dashboard of Code Insights](/code_insights/how-tos/creating_a_custom_dashboard_of_code_insights) - [Filtering an Insight](/code_insights/how-tos/filtering_an_insight) - [Troubleshooting Code Insights](/code_insights/how-tos/Troubleshooting) # Filtering a code insight This how-to assumes that you already have [created some search insights](/code_insights/quickstart). > NOTE: filters are not yet available on language statistics insights. ### 1. Click the filter icon button for the insight you want to filter While viewing the insight you want to filter, click the filter icon in the upper right corner of the insight. This will open the filter popover. ### 2. Filter to include or exclude repositories using a regular expression The filter popover gives you two inputs that allow you to to filter the repositories contributing to the insight, either via inclusion with `repo:` or exclusion with `-repo:`. Enter a regular expression for repository names that you'd like to include or exclude. Inclusion filters limit your insight to show data from only given repository matches. Exclusion filters filter out results from the repository matches. ([More details on how repo filters are applied](/code_insights/explanations/code_insights_filters#repo-repo-filters).) The regular expression to filter for a repository works the same as the [`repo:` filter in the search box](/code-search/queries#filters-all-searches). Examples: | Pattern | Explanation | | | | --------------------------------------- | --------------------------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------ | | `^github\.com/sourcegraph/sourcegraph$` | Filter the specific repository `github.com/sourcegraph/sourcegraph` | | | | `^github\.com/sourcegraph/(sourcegraph\ | about\ | docsite)$` | Filter the specific repositories `github.com/sourcegraph/sourcegraph`, `github.com/sourcegraph/about` and `github.com/sourcegraph/docsite` | | `^github\.com/sourcegraph/go-` | Filter all repositories that start with `github.com/sourcegraph/go-` | | | | `service` | Filter all repositories that contain the word `service` in their name | | | | `\.js$` | Filter all repositories that end in `.js` | | | ### 3. Or, filter to include a reusable group of repositories using a query-based search context In the `context:` field of the filter panel, you can use a [query-based search context](/code-search/working/search_contexts#beta-query-based-search-contexts) to filter your insights to only results matching repositories that match the query-based context's `repo:` filter. First, if you haven't already created a search context, create a [query-based search context](/code-search/working/search_contexts#beta-query-based-search-contexts). You can define any group of repos using the syntax `repo:(^github\.com/sourcegraph/sourcegraph$|^github\.com/sourcegraph/about$...)`. Then, in the context field, reference the search context's name, usually `@owner/name-of-context`. ([More details on using search contexts as filters, and currently supported search filters](/code_insights/explanations/code_insights_filters#context-query-based-search-context-filters).) ### 4. Close the filter panel You can safely click outside the panel to close it and your filters will remain (until a page reload). A dot next to the filter icon indicates that an insight currently has filters applied. ### 5. Edit or reset filters Click the filter button again to edit or reset filters. ### 6. Save your filters as defaults on this insight (optional) By default, the filter will only be visible to you locally and will reset after a page reload. To persist the filter so that anyone who views the insight will have the filters applied by default, click "Save/update default filters". ### 7. Save as new view (optional) If you don't want to modify the insight for all future viewers, but still want to preserve your filtered view, you can select "save as new view." This will fork the insight and create a separate chart on the dashboard with these filters applied by default. The original insight will be unmodified, and future edits to either insight will be independent. # Creating a custom dashboard of code insights This how-to assumes that you already have created some code insights to add to a dashboard. If you have yet to create any code insights, start with the [quickstart](/code_insights/quickstart) guide. ## 1. Navigate to the Code Insights page Start on the code insights page by clicking the Code Insights navbar item or going to `/insights/all`. ## 2. Create and name a new dashboard Click "Create new dashboard" in the top right corner and name your dashboard. Dashboard names must be unique. ## 3. Select a visibility level Set a visibility level for your dashboard. Dashboards [respect insights' permissions](/code_insights/explanations/viewing_code_insights#dashboard-visibility-respects-insights-visibility), so don't create an organization-shared dashboard if you have private insights you want to attach to it. - Private: visible only to you - Shared with [an organization](/admin/organizations): visible to everyone in the organization - Global: visible to everyone on the Sourcegraph instance Then click "Create dashboard." ## 4. Add insights to your new, empty dashboard Click the "Add insight" box on the empty dashboard view to pull up the add insights modal. You can also always pull this modal up with the contextual three-dots click to the right of the dashboard dropdown picker, via the "Add or remove insights" option. Select which insights you want to add. Insights won't be added until you click save. Un-checking an insight will remove that insight after you click save. ## 5. Share your dashboard You can share your dashboard via the url, or by copying this same url in the "copy link" menu item next to the dashboards picker. # Troubleshooting Code Insights This is a collection of issues or other information that might be helpful when troubleshooting a problem with Code Insights. ## Recurring OOM (out of memory) alerts from the `frontend` service This may be the result of an excessively large query being executed by code insights. Code Insights processes some queries in the background of the `worker` service. These queries use the GraphQL API, which means they are aggregated entirely on the `frontend` service. Large result sets can cause the `frontend` service to run out of memory and crash with an OOM error. These queries can get stuck in an error loop until they hit the maximum retry value, causing repeated `frontend` crashes. Queries such as matching on every line in every repository or other queries with a similar scale may be responsible. ### Diagnose 1. Check the `frontend` dashboards (General / Frontend) in Grafana 1. Check for individual instances with spiked (to 100%) memory usage on `Container monitoring` - `Container memory by instance` 2. Check the background `worker` dashboards for Code Insights (General / Worker) in Grafana 1. Check for elevated error rates on `Codeinsights: dbstore stats` - `Aggregate store operation error rate over 5m` 2. Check for a queue size greater than zero on `Codeinsights: Query Runner Queue` - `Code insights query runner queue size` 3. (admin-only) Check the queries currently in background processing using the GraphQL query ``` gql query seriesStatus { insightSeriesQueryStatus { seriesId query enabled completed errored processing failed queued } } ``` 1. Inspecting queries with `errored` or `failed` counts may provide a hint to which query is responsible. 4. Check Postgres `pgsql` for any queries stuck in a retry loop ``` sql select * from insights_query_runner_jobs where state = 'errored' and started_at > current_timestamp - INTERVAL '1 day' order by insights_query_runner_jobs.started_at desc; ``` ### Resolution Options 1. Increase the memory available to the `frontend` pods until it is sufficiently large enough to execute the responsible query. 1. The error rate on the Code Insights dashboards should return to zero. 2. (admin-only) Disable any specific queries identified to be problematic using the GraphQL operation by providing a specific `SeriesId`. ``` gql mutation updateInsightSeries($input: UpdateInsightSeriesInput!) { updateInsightSeries(input:$input) { series { seriesId query enabled } } } { "input": { "seriesId": "s:5FE04D15D1150A134407E7EF078028F6DA5224BBADB1718A92E46046AC9F2E0B", "enabled": false } } ``` 3. Disable any problematic queries stuck in an error loop in Postgres `pgsql` ```sql update insights_query_runner_jobs set state = 'failed' where id = ?; ``` ## OOB Migration has made progress, but is stuck before reaching 100% This out-of-band migration is titled: **Migrating insight definitions from settings files to database tables as a last stage to use the GraphQL API.** The out-of-band migration shouldn't take more than an hour to complete. (It really shouldn't take more than a few minutes.) If the progress hasn't reached 100% in this duration some records may be stuck due to errors. Known issues: - Deleted users/orgs will cause processing errors, and those jobs will need to be manually marked as complete. ### Diagnose and Resolve 1. First check the Recent Errors under the migration in the UI. 1. If the error messages are all: `UserStoreGetById: user not found` - This is caused by deleted users. It will be safe to mark these rows as completed by running the following against `pgsql`: ```sql UPDATE insights_settings_migration_jobs SET completed_at = NOW() WHERE completed_at IS NULL; ``` 2. If the error messages are all: `OrgStoreGetByID: org not found` - This is caused by deleted orgs. In this case, mark just the org rows as completed by running the following against `pgsql`: ```sql UPDATE insights_settings_migration_jobs SET completed_at = NOW() WHERE completed_at IS NULL AND org_id IS NOT NULL; ``` - Note: this only completes the failing org jobs. You may then see the `user not found` error above, and will still need to mark the rest of the jobs as complete. 3. If the error messages are neither of those two things, this is not currently a known issue. Contact support and we can help! ## Clicking on Code Insight data points leads to invalid search queries ### Issue When clicking on a data point in a Code Insight, users are redirected to the search results page with an invalid search query, resulting in no results or an error message. ### Symptoms - Clicking on a Code Insight data point leads to a search query with too many slashes - The search query fails with an error message like: "Unable To Process Query Error parsing regexp: missing closing ): (/" - No search results are displayed ### Workaround Modify your Code Insight query to use the new syntax style. For example: Instead of: ``` select:file patterntype:regexp file:.php content:class\s(.)\sextends\sModelRepo\s{ ``` Use: ``` select:file file:.php /class\s(.)\sextends\sModelRepo\s{/ ``` ### Steps to apply the workaround 1. Navigate to your Code Insight 2. Locate the query causing the issue 3. Update the query using the new syntax style as shown above 4. Save the changes to your Code Insight After applying this workaround, clicking on data points should lead to valid search queries and display the expected results. # Viewing Code Insights ## Insights dashboards The main way to view code insights is on a dashboard page. Dashboards have a unique name and visibility level. There are three possible visibility levels: - Private: visible only to you - Shared with [an organization](/admin/organizations): visible to everyone in the organization - Global: visible to everyone on the Sourcegraph instance ## Sharing links to individual insights You can share links to individual insights by clicking the three-dots context menu on an insight and selecting the shareable link. In order to share a code insight with someone else, the insight must already be on a dashboard visible to them: either a global or organization's dashboard. The share link box will indicate which other users will be able to view the insight, or if the insight is private and must first be added to a more public dashboard in order to share it. ### Built-in dashboards A default "dashboard" of all insights visible to a user appears as the homepage for the Insights navigation bar item. To add insights to your own custom dashboard, see [Creating a custom dashboard of code insights](/code_insights/how-tos/creating_a_custom_dashboard_of_code_insights). ### Dashboard visibility A Dashboard's visibility level owns the visibility of code insights on the dashboard. If you add an Insight that was on a private dashboard to an organization or global dashboard, now people with access to that dashboard can view the insight. When you first create an insight, it defaults to appearing on the dashboard from which you clicked the "create" button, and inherits that original dashboard's permissions. Everyone who can access an insight to view it can also edit it. If you create an insight directly on the create page or from the default dashboard, it will appear on the default dashboard and default to "private" permissions. There's one exception: if the instance has no code insights license, then private dashboards and insights are disallowed, and all limited access mode insights are global. ### Insights still enforce individual permissions regardless of dashboard visibility The dashboard visibility levels have no impact on the data in the insight itself that is displayed to an individual user: insights [enforce the viewer's permissions](/code_insights/explanations/administration_and_security_of_code_insights#code-insights-enforce-user-permissions). This means that two organization users with different repo read permission sets might see different values for the same insights on the same dashboards, if it contains results from a repository only one user can view. If you change a private-visible dashboard so that the dashboard is now visible to an organization (or globally), then the organization (or entire instance) can now see all insights on the dashboard, though the result counts will still be filtered to count from only repos that the individual organization member can view. ### Insights can be on multiple dashboards You can attach insights to multiple dashboards. # Search results aggregations In version 4.0 and later, Code Insights provides aggregations shown on the search screen. This lets you track version and license spread, library adoption, common commit messages, lengths of specific files, usage frequencies, and the [many common use case examples here](../references/search_aggregations_use_cases.md). ## Available aggregations: You can aggregate any search by: 1. The repositories with search results 1. The files with search results (for non-commit and non-diff searches) 1. The authors who created the search results (for commit and diff searches) 1. All found matches for the first capture group pattern (for regexp searches with a capture group) Aggregations are returned in order of greatest to least results count. Aggregations are exhaustive across all repositories the user running the search has access to, unless the chart notes otherwise (see [Limitations](#limitations) below). We may continue adding new aggregation categories, like date and code host, based on feedback. If there are categories you'd like to see, please [let us know](mailto:feedback@sourcegraph.com). ## Feature visibility You can turn the aggregations on with the experimental feature setting: `searchResultsAggregations` in your user, org, or site settings. You can turn off just the proactive aggregations by setting `proactiveSearchResultsAggregations` to `false`. This prevents aggregations from running on every search and requires users to explicitly click to run them. (The main reason to consider disabling proactive aggregations is if you're seeing a heavy or unexpected load on your instance, but as noted below in [Limitations](#limitations) there are limits that keep the overall resource needs low to begin with.) ## Drilldowns You can drilldown into a search aggregation by clicking a result in the chart. Your original search query will be updated with a `repo`, `file`, `author` filter or a regexp pattern depending on the aggregation mode. ## Limitations ### Mode limitations If you attempt to run a query for which a given mode is not supported, the tooltip will inform you why that mode is not available. ### Timeout limits At the moment all aggregations search queries are run with a 2-second timeout, even if your search specified a timeout. If the aggregation times out, you will be able to trigger a longer search with a 1-minute timeout by clicking `Run aggregation`. The extended timeout can be configured by changing the number of seconds in global settings `insights.aggregations.extendedTimeout`. If configuring this to greater than 60 seconds please see [More information on timeouts](../../code_search/how-to/exhaustive.md#timeouts). After adding new repositories it can be common for aggregations to experience timeouts while those repositories await initial indexing. This is due to aggregations running exhaustive searches over all repositories and will resolve once that indexing is complete. ### Count limits Aggregation search queries that run proactively are run with `count:50000`. This default can be changed using the site setting `insights.aggregations.proactiveResultLimit`. If the number of results exceeds this limit, the user can choose to explicitly run the aggregation, and these explicitly-run aggregations use `count:all`. ### Best effort aggregation Results are aggregated in a best-effort approach using a limited-size buffer to hold group labels in order not to strain the webapp when these aggregations are run. This means that in some cases we might miss some high-count results. Aggregations that hit such non-exhaustive paths are reported back to the user. You can control the size of the buffer using the site setting `insights.aggregations.bufferSize`. It is set to 500 by default. Note that if increasing this you might notice decreased performance on your instance. ### Number of bars displayed The side panel will display a maximum of 10 bars. If expanded, a maximum of 30 bars will be displayed. If there are more results this will be displayed on the panel. ### Single capture group Aggregations by capture group will match on the first capture group in the search query only. For example, for a query: ```sgquery hello-(\w+)-(\w+) ``` and a match like `hello-beautiful-world` only `beautiful` will be shown as a result. ### Files with the same paths in distinct repositories The "file" aggregation groups only by path, not by repository, meaning files with the same path but from different repos will be grouped together. Attach a `repo:` filter to your search to focus on a specific repo. ### Saving aggregations to a code insights dashboard Saving aggregations to a dashboard of code insights is not yet available. ### Slower diff and commit queries Running aggregations by author is only allowed for `type:diff` and `type:commit` queries, which are likely not to complete within a 2-second timeout. You can trigger an explicit search with an extended 1-minute timeout, or you can limit your query using a single-repo filter (like `repo:^github\.com/sourcegraph/sourcegraph$`) combined with a `before` or `after` filter. ### Structural searches Aggregations for structural searches are unlikely to complete within a 2-second timeout. You can try to trigger an explicit aggregation for such cases. ### Standard searches with embedded regexp Standard searches with embedded regexp such as below do not support aggregation by capture group. This is because they are functionally similar to a query with an `or` operator. ```sgquery database /(\w+)/ ``` # Explanations The following articles explain different parts of Sourcegraph Code Insights in depth: - [Administration and Security of Code Insights](/code_insights/explanations/administration_and_security_of_code_insights) - [Automatically generated data series for version or pattern tracking](/code_insights/explanations/automatically_generated_data_series) - [Code Insights filters](/code_insights/explanations/code_insights_filters) - [Current limitations of Code Insights](/code_insights/explanations/current_limitations_of_code_insights) - [Search-screen search results aggregations](/code_insights/explanations/search_results_aggregations) - [Viewing code insights](/code_insights/explanations/viewing_code_insights) - [Data retention](/code_insights/explanations/data_retention) # Code Insights data retention > NOTE: This reference is only relevant from the 4.5 release. On creation a Code Insight will show you 12 data points per series. Your Code Insight will then get an additional ephemeral data point daily, and a persisted additional data point at every interval that was specified on insight creation. Prior to release 4.5, this growth was unbound. From 4.5, the oldest data points will be truncated and stored separately according to the sample size specified in the site configuration with the setting shown below. This means that if you have an insight with 50 data points, and a maximum sample size of 30, the oldest 20 points will be truncated and archived in a separate table. ```json { "insights.maximumSampleSize": 30 } ``` The sample size value is set to 30 by default and can only be increased to a maximum of 90. A background routine will periodically go over Code Insights and move the oldest data points to a separate table. If you want to access this data, you are given the option to export all data for a Code Insight, which will include archived data. > IMPORTANT: Your data is never deleted. It is only moved to a separate database table. ## Data exporting You can download all data for a Code Insight, including data that has been archived. You can do this: - From the insight card menu - From the standalone page - By curling the API endpoint ```shell curl \ -H 'Authorization: token {SOURCEGRAPH_TOKEN}' \ https://yourinstance.sourcegraph.com/.api/insights/export/{YOUR_INSIGHT_ID} -O -J ``` The data will be exported as a CSV file. Only data that you are permitted to see will be excluded (i.e. repository permissions are enforced). If you have filtered your Code Insight using repository filters or a search context, the data exported will be filtered according to those. ## Dynamic filtering The option now exists on Code Insights filters to limit the number of samples loaded per series. There is one setting per insight that applies to all series on that insight. Adjusting this setting will only apply to one insight and will not have any impact on stored Code Insights data. ## Benefits - Code Insights should be faster to load. - Code Insights with a lot of data points that were previously hard to read or hover over will now be more legible. - Code Insights data can now be exported in CSV format. ## Accessing this feature prior to 4.5 You can enable this retention procedure from Sourcegraph *4.4* if you are a site admin with the following setting: ```json { "experimentalFeatures": { "insightsDataRetention": true } } ``` You will however **only be able to export all the code insights data from the 4.5 release**, so use the experimental version at your own risk. # Current limitations of Code Insights There are a few existing limitations. If you have strong feedback, please do [let us know](mailto:feedback@sourcegraph.com). _Limitations that are no longer current are [documented at the bottom](#older-versions-limitations) for the benefit of customers who have not yet upgraded._ ## Insight chart position and size do not persist You can resize and reorder charts on the dashboard for the purpose of taking a screenshot or presenting information, but that order will revert on a page refresh. If the ordering of insights is important, you can remove and then re-add the insights in the order you'd like via the add/remove insights to dashboard flow. If the size is important, you can use the single-insight view page to consistently view an insight at a larger size, reachable by clicking the insight title or from the context three dots menu on the insight card under "Get shareable link". ## Performance speed considerations for a data series running over many repositories To accurately return historical data for insights running over many repositories, the backend service must run a large number of Sourcegraph searches. This means that unlike code insights running over just a few repositories, results are not returned instantly, but more often on the scale of 20-120 minutes, depending on: * How many and how large the repositories you have connected to your instance * The performance and resources of your Sourcegraph code insights instance in queries-per-second * How well we can "compress" repositories so we don't need to run queries for each historic data point (e.g., if a repository hasn't changed in several months) The number of insights you have does not affect the overall speed at which they run: it will take the same total time to run all of them whether or not you let each one finish before creating the next one. As of version 4.4.0 Insights prioritize completing the backfills for the insights that will complete the fastest. In general this means that insights over many repositories will pause to allow insights over a few repositories to complete. ## Creating language insights for a very large repository > NOTE: This applies to Sourcegraph versions greater than `5.3` Similar to [insights in general](#creating-insights-over-very-large-repositories), creating a language insight over a very large repository can be slow. Language insights become faster as the internal cache populates, but depending on your Sourcegraph instance and repository size this may take a few attempts. By default the dashboard attempts three queries that take up to 5 minutes. It will automatically retry until the three attempts are exhausted. Apart from waiting and retrying you may also reach out to your Sourcegraph administrator to [increase the number of concurrent queries or increase the timeout for the query](/code_insights/explanations/administration_and_security_of_code_insights). ## Creating insights over very large repositories > NOTE: The feature applies on Sourcegraph version greater than `3.42` In some cases, depending on the size of the Sourcegraph instance and the size of the repo, you may see odd behavior or timeout errors if you try to create a code insight running over a single large repository. In this case, it's best to try: 1. Create the insight, but check the box to "run over all repositories." (This sends the Insight backfilling jobs to the backend Sourcegraph instance worker which will handle them datapoint-by-datapoint. Running over an individual repository otherwise currently runs the jobs in bulk to generate its live preview.) 2. After the insight has finished running, [filter the insight](/code_insights/explanations/code_insights_filters#filter-options) to the specific repo you originally wanted to use. The filter resolves instantly. If this does not solve your problem, please reach out directly to your Sourcegraph contact or in your shared slack channel, as there are experimental solutions we are currently working on to further improve our handling of large repositories. ## Accuracy considerations for an insight query returning a large result set If you create an insight with a search query that returns a large result set that exceeds the search timeout (generally when there are over 1,000,000 results), non-historical data points may report undercounted numbers. This is because non-historical data points are recorded with a global search query as opposed to per-repo queries we run for backfilling. For a large result set (e.g. a query for `test` with millions of results) the global query will be disadvantaged by the global search timeout. You can find more information on search timeouts in the [docs](/code-search/queries/language#timeout). You can determine if this issue may be affecting your query by just running the query in the Search UI on `/search` with a `count:all` – if your search is returning `x results in 60s` (or the upper limit max timeout is configured to) then the search will time out on insights as well. Note that the duration could be more or less `60s`, e.g. you could encounter `60.02s` as well. In this case, you may want to try: * Using a more granular query * Changing your site configuration so that the timeout is increased, provided your instance setup allows it. [More information on timeouts](/code-search/queries/language#timeout). ## General scale limitations Code Insights is disabled by default on single-docker deployment methods. There are a few factors to consider with respect to scale and expected performance. 1. General permissiveness - instances that are more open (users can see most repos) will perform better than instances that are more restricted. Insights have been tested with users having up to 100k restricted repositories. 2. Number of repositories - Code Insights is well tested with insights running over ~35,000 repositories. It is recommended that users should scope their insights to the smallest set of repositories needed. Users should expect at least linear degradation as repository count grows in both time to calculate insights, and render performance. 3. Large monorepos - Code Insights allocates a fixed amount of time for each query, so large repositories that cause query timeouts will likely not have exhaustive (and therefore accurate) results. As of version 4.4.0 we added visibility to this state via an icon on the insight. Prior to the 4.4.0 version a heuristic indicator for if this is a problem is seeing values "jump" (either a significant increase or decrease) between the backfilled datapoints on creation and the up-to-date datapoints added after creation. 4. High cardinality capture groups - When using a capture group insight, high cardinality matches (for example 1000 distinct matches per repository) will cause significant increase in loading times of charts. It is possible to exceed request timeouts if there are too many distinct matches. 5. Concurrent usage 1. If there are many insight creators the insights will take longer to calculate. 2. If there are more insight viewers loading times of charts may be impacted. ## Creating insights over specific branches and revisions Code Insights does not yet support running over specific revisions. ## VCS limitations Code Insights only supports git based repositories and does not support perforce repositories that have sub-repo permissions enabled. Perforce depots converted to git are also currently not supported for Code Insights. ## Feature parity limitations ### Features currently available only on insights over all your repositories * **[Filtering insights](/code_insights/explanations/code_insights_filters)**: available in 3.41+ ~~we do not yet allow filtering for insights that run over explicitly defined lists of repositories, except for "detect and track" insights.~~ ### Features currently available only on insights over explicitly defined repository lists Because these insights need to run dramatically fewer queries than insights over thousands of repositories, you will have access to a number of features not _yet_ supported for insights over all repositories. These are: * **Live previews**: showing the preview of your insight in real time * **[Released] Dynamic x-axis ranges**: available in 3.35+ ~~set a custom amount of historical data you care about~~ * **[Released] Editing data series queries after creation**: available in 3.35+ ~~for insights over all repositories, you must make a new insight if you wish to run a different query~~ * **[Released] "Diff click"**: available in 3.36+ ~~click a datapoint on your insight and be taken to a diff search showing any changes contributing to the difference between a datapoint and the prior one~~ > NOTE: many of the above-listed features will become available for insights over all repositories as well. The above list is ordererd top-down, where items on the top of the list will arrive roughly sooner than items on the bottom. ### Limitations specific to "Detect and track patterns" insights (automatically generated data series) Please see [Current limitations of automatically generated data series](/code_insights/explanations/automatically_generated_data_series#current-limitations). ## In certain cases, chart datapoints don't match the result count of a Sourcegraph search There are currently a few subtle differences in how code insights and Sourcegraph web app searches handle defaults when searching over all repositories. Refer to [Common reasons code insights may not match search results](/code_insights/references/common_reasons_code_insights_may_not_match_search_results). ## Older versions' limitations ### Version 3.30 (July 2021) or older #### Search-based Code Insights can only run over ~50-70 repositories Because this version of the prototype runs on frontend API calls to Sourcegraph searches, it may run slowly (or possibly timeout) if you're using it over many repositories or with many data series for each insight. #### The max match count is 5,000 matches per repository The current limit on searching over historical versions of repositories, which is an unindexed search, is 5,000 results per repository. If there are more than 5,000 matches, the search stops and returns a count of 5,000, and the code insight graph will calculate the overall chart using 5,000 as the match count for that repository. (This means if you query over two repositories and one of them hits this limit, the value shown on the graph will be 5,000 + [the match count in the other repository]). _This limit was lifted in the August 2021 release of Sourcegraph `3.31` # Code Insights filters Code insights filters allow you to filter a search insight to a subset of repositories. Each search insight has a filter icon that opens the filtering options. Filters take effect immediately, without needing to re-process the data series of the insight. > NOTE: filters are not yet available on language statistics insights. ## Filter options ### `repo:` Repo filters You can include or exclude repositories from your insight using regular expressions, the same way you can with the `repo:` filter in [Sourcegraph searches](/code-search/queries#filters-all-searches). Only repository regex expressions are supported: unlike search, you cannot specify repository revisions using the syntax `repo:regexp-pattern@rev`. Predicate filters like `repo:has.path(...)` are also not supported. For inclusion, only repositories that have a repository name matching the regular expression will be counted. For exclusion, only repositories that have a repository name **not** matching the regular expression will be counted. If you combine both filters, the inclusion pattern will be applied first, then the exclusion pattern. ### `context:` Query-based search context filters You can use a [query-based search context](/code-search/working/search_contexts#beta-query-based-search-contexts) to filter your insights to only results matching repositories that match the `repo:` or `-repo:` filter of the query-based context. You can use this to filter multiple insights to a group of repositories that need only be maintained in one location. When you update a context that's being used as a filter, the next time you load the page, the filtered insight will reflect the updated context. As with explicit `repo:` filters, only repository regex expressions are allowed: you cannot specify repository revisions or predicate filters. At this time, all other filters are not yet supported – only the `repo:` and `-repo:` filters of a context are recognized. When creating your context, you can define any group of repos using the syntax `repo:(^github\.com/sourcegraph/sourcegraph$|^github\.com/sourcegraph/about$...)`. ### Setting more than one filter Filters are intersected. This means if you use the search context `context:` filter that narrows your insight down from all repositories to some repo A, repo B, and repo C, and then you use the `repo:` filter that's set only to repo A, your insight will show only results from repo A. Similarly, if you were to use the same `context:` and then also use the `-repo:` exclusion filter set to repo C, your insight would show results from repo A and repo B. ### Other filtering options We're currently exploring additional filters that would be valuable. If you have feedback about a particular filter you'd like for code insights, we would [love to hear your feedback](mailto:feedback@sourcegraph.com). ## Filter persistance and sharing ### Filters are temporary by default By default, filters are temporary (present until you refresh the page or switch to a different dashboard) and local (no one else can see them). You can modify filters without affecting what others see, unless or until you save the filters. ### Saving filters as defaults You can set filters to be persistent, or default, even after a page reload on a code insights in two ways: 1. Create a filter and click "save/update default filters": this will save the filters so they persist for all viewers of this insight, on any dashboard page the insight appears. 1. Create a filter and "save as new view": this will create a new insight chart with your filter applied as the default filter on that insight. It will _not_ also save these filters to the existing insight unless you also select "save/update default filters". ### Filter indicator Insights that have any filters applied will have a small dot on their filter icon. The filter can always be reset by opening the filter panel again and clicking "Reset" above each filter – but note that, like any filter edit, you must also then "update default filters" to save that reset state if you want it to persist. ### Saving a filter as a new view When you create a filter and "save as new view," it will create a new insight chart with this filter saved as the default filters. Except the title and filters, all other configuration options will be cloned from the original insight. Filters and all other configuration options for the newly created view and the original insight are indpendent. Editing the forked insight (e.g. changing the data series query) will not change the original insight. # Detect and track patterns with automatically generated data series Detect and track patterns' insights are available in Sourcegraph v3.35 and above. In Sourcegraph v3.35 this feature is in its earliest version. Stability and additional features coming in following releases. Code Insights with automatically generated data series allow you to "detect and track patterns" and versions within your codebase. You can use these insights to track versions of languages, packages, terraform, docker images, or anything else that can be captured with a [regular expression capture group](#regular-expression-capture-group-resources). ## Automatic generation of individual series Match patterns can be returned with a regular expression containing a capture group, or a pattern within parentheses. For example, `file:pom\.xml|\.pom$ (.*)` will match all of: ```shell 1.5 1.7 1.8 ``` Code Insights will find all matches, and then automatically generate a data series and color for each unique value of the capture group. In this case, the chart would show data series for 1.5, 1.7, and 1.8, with the values being the number of matches for each unique value. ## New matching data gets automatically added Capture groups will automatically create new data series for new matches as they appear in your codebase. You do not need to update or manually re-create the insights to track newly added versions or patterns. For the above example, this means that if `1.9` was committed to the codebase in the future, it would appear on the insight without any additional action, and you would see a series for `1.9`. ## Current limitations This feature has some yet-released limitations. In rough order, with limitations listed first likely to be removed soonest, they are: ### Limited to 20 matches Capture groups will only display 20 returned match values to prevent extremely large result sets from being rendered. As of version 3.41.0 controls are available to make this deterministic and configurable. ### No capture groups in filter strings This type of Code Insight only supports capture groups in the main query string. You cannot use a capture group in a filter, like a `repo:` or `file:` filter. ### No multiline matches >Note: Feature supported only on version greater than 3.43. On Sourcegraph instances on earlier versions than 3.43, you can only use a single-line regular expression. This means that `^` and `$` characters are still valid but needing to match on a `\n` is not. As a potential workaround to get more granularity, you can still use `file:has.content()` and other [search predicates](/code-search/queries/language#built-in-file-predicate). ### No combinations of capture groups You cannot combinatorially combine capture groups. Queries containing multiple capture groups will return data series with a label for a single capture group only. For example, `([0-9])\.([0-9])\.([0-9])` will match results like 1.2.3, 4.5.6, and 4.5.7, but will return data series for 1, 2, 3, 4, 5, 6, and 7 individually. (If you made the string one capture group `([0-9]\.[0-9]\.[0-9])` instead, it would return series and counts for 1.2.3, 4.5.6, and 4.5.7). ### Searches are limited to file content You can't use capture groups in queries for `type:commit`, `type:repo`, `type:path`, `type:diff`, or `type:symbol`. ### Match values are limited to a 100 characters Any matches that return values over a 100 characters will be truncated. This is to avoid issues when storing the data. ### Regular expression capture group resources - [Example regular expressions for common use cases](/code_insights/references/common_use_cases#automatic-version-and-pattern-tracking) - [General additional capture groups documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges) # Administration and security of Code Insights ## Code Insights enforce user permissions Users can only create code insights that include repositories they have access to. Moreover, when creating code insights, the repository field will *not* validate nor show users repositories they would not have access to otherwise. When a user is viewing an insight, any repositories they do not have access to will not be included in the total counts. ## Security of native Sourcegraph Code Insights (Search-based and Language Insights) Sourcegraph search-based and language insights run natively on a Sourcegraph instance using the instance's Sourcegraph search API. This means they don't send any information about your code to third-party servers. ## Insight and Dashboard permissions Note: there are no separate read/write permissions. If a user can view an insight or dashboard, they can also edit it. A user can view an insight if at least one of the following is true: 1. The user created the insight. 2. The user has permission to view a dashboard that the insight is on. Except for the singular, non-transferable creator's permission noted in case 1 above, permissions can be thought of as belonging to a dashboard. Dashboards have 3 permission levels: - User: only this specific user can view this dashboard. - Organization: only users within this organization can view this dashboard. - Global: any user on the Sourcegraph instance can view this dashboard. ### Changing permission levels Because there are no separate read/write permissions and no dashboard owners, any user who can view a dashboard can change its permission level or add/remove insights from the dashboard. The only way to guarantee continued access to an insight that you did not create is to add it to a private dashboard. If a user gets deleted, any insights they created will still be visible to other users via the dashboards they appear on. However, if one of these insights is removed from all dashboards, it will no longer be accessible. ## Code Insights Admin (>=5.0) Sourcegraph administrators can view and manage the background jobs that Code Insights runs when historically backfilling an insight. The following functionality is available under _Code Insights jobs_ in the _Maintenance_ section within the Site Admin (`/site-admin/code-insights-jobs`): - See a list of the jobs that backfill an insight and their current state - See any errors that occurred when backfilling an insight - Retry a failed backfill - Move a backfill job to the front or back of the processing queue The jobs can be searched by state and by an insight's title or the label of any of its series. ## Code Insights Site Configuration While the default configuration is appropriate for most deployments, in the site configuration there are values that allow admins more control over the rate at which insights runs in the background. Raising these values will increase the speed at which insights are populated however will it cause insights to consume more system resources. Care should be taken when changing these values and it is recommended to update them in small increments. The following settings apply when backfilling data for a Code Insight: - `insights.historical.worker.rateLimit` - Maximum number of historical Code Insights data frames that may be analyzed per second. - `insights.backfill.interruptAfter` - The amount of time an Code Insights will spend backfilling a series before checking if there is higher priority work. - `insights.backfill.repositoryGroupSize` - The number of repositories that Code Insights will pull as a batch to backfill in one iteration. - `insights.backfill.repositoryConcurrency` - The number of repositories that Code Insights will backfill at once. The following setting(s) apply to adding new data to a previously backfilled Code Insight: - `insights.query.worker.concurrency` - Number of concurrent executions of a code insight query on a worker node. The following setting(s) apply to both backfilling data and adding new data - `insights.query.worker.rateLimit` - Maximum number of Code Insights queries initiated per second on a worker node. ## Language Stats Performance Configuration This feature is supported for Sourcegraph versions 5.4 or more. To create language stats for a repository the Sourcegraph instance analyzes all files from the target repository. Depending on the repository's size this can take from a few seconds to multiple minutes. If you need to analyze a repository that's larger than 10GB feel free to reach out to Sourcegraph support. If a query completes the result is stored in an API-level cache and subsequent queries complete within one second. If the query does not complete in time, an internal cache is stays partially populated. This cache will be reused on subsequent queries which then have less work to do and may resolve faster. With the May 2024 Release we increased the default number of concurrent requests to the gitserver from 1 to 4, and raised the timeout for each language stats query from 3 minutes to 5 minutes. ### Concurrent Requests This concurrent requests to the gitserver are configurable through the `GET_INVENTORY_GIT_SERVER_CONCURRENCY` environment variable. We recommend increasing this carefully, as an increase in concurrency may cause the gitserver to become overloaded and slow down responses. Example: ``` GET_INVENTORY_GIT_SERVER_CONCURRENCY=4 ``` To understand how this configuration impacts your language stats queries you can use [tracing](/admin/observability/tracing). ### Language Stats Timeout The timeout in minutes for language stats queries is configurable through the `GET_INVENTORY_TIMEOUT` environment variable. Example: ``` GET_INVENTORY_TIMEOUT=5 ``` # Code Search Supported on [Enterprise Starter](/pricing/enterprise-starter) and [Enterprise](/pricing/enterprise) plans. Available via VS Code and JetBrains editor extensions and the Web.

Learn how to search code across all your repositories and code hosts.

**Code Search** allows you to find, fix, and navigate code with any code host or language across multiple repositories with real-time updates. It deeply understands your code, prioritizing the most relevant results for an enhanced search experience. Sourcegraph's Code Search empowers you to: - Utilize regular expressions, boolean operations, and keyboard shortcuts to help you unleash the full potential of your searches - With the symbol, commit, and diff search capabilities, it identifies code vulnerabilities in milliseconds and quickly helps you resolve issues and incidents - Offers innovative code view with seamless code navigation for a comprehensive coding experience ## Getting started ## Features Code Search main features include: - Use regular expressions and keyword queries to perform full-text searches - Search any branch and commit, with no indexing required - Search [commit diffs](/code-search/features#commit-diff-search) and [commit messages](/code-search/features#commit-message-search) to see how code has changed - Narrow your search by repository and file pattern - Use [search contexts](/code-search/features#search-contexts) to search across a set of repositories at specific revisions - Curate [saved searches](/code-search/features#saved-searches) for yourself or your org - Use [code monitoring](/code_monitoring/) to set up notifications for code changes that match a query - View [language statistics](/code-search/features#statistics) for search results Read more in detail about the [Code Search features here →](/code-search/features) ## FAQs ### Does Code Search work with my repositories? Code Search works with all your repositories. Likewise, you can also [search through our public code](https://sourcegraph.com/search) that has a 2 million+ open source codebase. ### Do I need to enable Code Navigation? No, the default search-based code navigation works out of the box without any configuration. However, for an advanced and customized navigation experience your site admin will set up precise code navigation. ### What is the max file size limit for Code Search? By default, files larger than **1 MB** are excluded from search results. Read more in detail about other [Code Search FAQs here →](/code-search/faq) ## Join our community If you have any questions about Code Search, ask in [our community forum](https://community.sourcegraph.com).
# Code Search Capabilities

Learn and understand more about Sourcegraph's Code Search features and core functionality.

## Powerful, flexible queries Sourcegraph code search performs full-text searches and supports both regular expression and exact queries. By default, Sourcegraph searches across all your repositories. Our search query syntax allows for advanced queries, such as searching over any branch or commit, narrowing searches by programming language or file pattern, and more. See the [search query syntax](/code-search/queries) documentation for a comprehensive overview of supported syntax. ## Data freshness Searches scoped to specific repositories are always up-to-date. Sourcegraph automatically fetches repository contents with any user action specific to the repository and makes new commits and branches available for searching and browsing immediately. Unscoped search results over large repository sets may trail latest default branch revisions by some interval of time. This interval is a function of the number of repositories and the computational resources devoted to search indexing. ## Commit diff search Search over commit diffs using `type:diff` to see how your codebase has changed over time. This is often used to find changes to particular functions, classes, or areas of the codebase when debugging. You can also search within commit diffs on multiple branches by specifying the branches in a `repo:` field after the `@` sign. After the `@`, separate Git refs with `:`, specify Git ref globs by prefixing them with `*`, and exclude a commit reachable from a ref by prefixing it with `^`. Diff searches can be further narrowed down with parameters that filter by author and time. See the [query syntax](/code-search/queries#filters-diff-and-commit-searches-only) documentation for a comprehensive list of supported parameters. ## Commit message search Searching over commit messages is supported in Sourcegraph by adding `type:commit` to your search query. Separately, you can also use the `message:"any string"` parameter to filter `type:diff` searches for a given commit message. Commit message searches can narrowed down further with filters such as author and time. See our [query syntax](/code-search/queries#diff-and-commit-searches-only) documentation for a comprehensive list of supported parameters. ## Symbol search Searching for symbols makes it easier to find specific functions, variables, and more. Use the `type:symbol` filter to search for symbol results. Symbol results also appear in typeahead suggestions, so you can jump directly to symbols by name. When on an [indexed](/admin/search#indexed-search) commit, it uses Zoekt. Otherwise it uses the [symbols service](/code-search/types/symbol) ## Saved searches Saved searches let you save and describe search queries so you can easily monitor the results on an ongoing basis. You can create a saved search for anything, including diffs and commits across all branches of your repositories. Saved searches can be an early warning system for common problems in your code and a way to monitor best practices, the progress of refactors, etc. ## Search contexts Search contexts help you search the code you care about on Sourcegraph. A search context represents a set of repositories at specific revisions on a Sourcegraph instance that will be targeted by search queries by default. Every search on Sourcegraph uses a search context. Search contexts can be defined with the contexts selector shown in the search input, or entered directly in a search query. If you currently use version contexts, you can automatically [convert your existing version contexts to search contexts](/admin/how-to/converting-version-contexts-to-search-contexts). We recommend migrating to search contexts for a more intuitive, powerful search experience and the latest improvements and updates. See the [search contexts](/code-search/working/search_contexts) documentation to learn how to use and create search contexts. ## Multi-branch indexing The most common branch to search is your default branch. To speed up this common operation, Sourcegraph maintains an index of the source code on your default branch. Some organizations have other branches that are regularly searched. To speed up search for those branches, Sourcegraph can be configured to index up to 64 branches per repository. Your site admin can configure multi-branch indexing through [site configuration](/admin/search#multi-branch-indexing). ## Exclude files and directories You can exclude files and directories from search by adding the file `.sourcegraph/ignore` to the root directory of your repository. Files or directories matching the glob patterns will not show up in the search results. For more information on creating `.sourcegraph/ignore` files, see the [configuration reference](/admin/search#exclude-files-and-directories). ## RE2 Regular Expressions The Sourcegraph search language supports [RE2](https://golang.org/s/re2syntax) syntax. If you're used to tools like Perl which uses [PCRE syntax](https://www.pcre.org/original/doc/html/pcresyntax.html), you may notice that there are some features that are missing from RE2 like backreferences and lookarounds. We choose to use RE2 for a few reasons: - It makes it possible to build [worst-case linear](https://swtch.com/~rsc/regexp/regexp1.html) evaluation engines, which is very desirable for building a production-ready regex search engine. - It's well-supported in Go, allowing us to take advantage of a rich ecosystem (notably including [Zoekt](https://github.com/sourcegraph/zoekt)) - Our API and tooling makes it straightforward to use Sourcegraph with other tools that provide facilities not built in to the search language. As an example of how you can use Sourcegraph tooling with other tools, we can use `jq` (which supports Perl regexes) along with `src` to post-filter search results. In this case, we want to use backreferences to find go functions that take a single pointer argument and return a non-pointer of the same type as the input. ```shell re2_regex='func \w+\(\w+ \*\w+\) \w+' pcre2_regex='func \w+\(\w+ \*(\w+)\) \1' src search --json --stream -- "/$re2_regex/" \ | jq ' //Filter to only content events select(.type == "content") # Filter to only content events //Flatten to a single object per match | {content: .chunkMatches[].content} + del(.chunkMatches) //Select only matches that match the PCRE regex | select(.content | test($ARGS.positional[0])) ' --args "$pcre2_regex" ``` ## Search experience Users on Sourcegraph instance `v5.9.0` or more get the improved and new Code Search experience set by default. Sourcegraph Enterprise users' site admins can optionally opt-out and revert to the old view. You get the following improvements: - **In-line diff view**: Easily compare commits and see how a file changed over time, all in-line - **Revamped code navigation**: Quickly find a list of references of a given symbol, or immediately jump to the definition - **Reworked fuzzy finder ⌘K**: Find files and symbols quickly and easily with our whole new fuzzy finder - **File actions**: Like open in editor and open on code host ## Personalized search ranking Sourcegraph Enterprise users can get more personalized and better-ranked search results in the search bar for their codebases. With this feature, you get: - Improved ranking for keyword searches like "data router". This capability is enabled by default and cannot be configured - Personalized ranking, specifically boosted results from repos you recently contributed to. This is controlled by the setting `experimentalFeatures.boostRelevantRepositories` These boosted results from your recently contributed repositories make finding the code you care about easier. The personalized search ranking works best if you have large codebases, especially for new users who may otherwise get overwhelmed by results from unfamiliar or irrelevant repositories. Personalized search ranking is enabled by default, but you can disable it from your settings via: ```json experimentalFeatures: { boostRelevantRepositories: false } ``` Here is an example of search results with personalized search ranking enabled: ![search-ranking-enabled](https://storage.googleapis.com/sourcegraph-assets/Docs/personalized-search-rankings.png) As you can see, the results are now ranked based on their relevance to the query, and the results from repositories you've recently contributed to are boosted. ## Compare changes across revisions When you run a search, you can compare the results from two different revisions of the codebase. From your search query results page, click the three-dot **...** icon next to the **Contributors** tab. Then select the **Compare** option. From here, you can execute file and directory filtering and compare large diffs, making it easier to navigate and manage. This file picker is useful when comparing branches with thousands of changed files and allows you to select specific files or directories to focus on. You can [filter files directly](/code-search/compare-file-filtering) by constructing a URL with multiple file paths or use a compressed file list for even larger selections. ![file-and-directory-filtering](https://storage.googleapis.com/sourcegraph-assets/Docs/filter-by-file-dir-on-compare.png) ## Other search tips - When viewing a file or directory, press the `y` key to expand the URL to its canonical form (with the full 40-character Git commit SHA). - To share a link to multi-line range in a file, click on the starting line number and shift-click on the ending line number (in the left-hand gutter).
# FAQs

Learn about some of the most commonly asked questions about Code Search.

## Code Search ### Does Code Search work with my repositories? Code Search works with all your repositories. Likewise, you can also [search through our public code](https://sourcegraph.com/search) that has a 2 million+ open source codebase. ### Who can search my code? Public code is searchable by anyone, but your private code can be searched only by users who have access to it. ### Do I need to enable Code Navigation? No, the default search-based code navigation works out of the box without any configuration. However, for an advanced and customized navigation experience your site admin will set up precise code navigation. ### What is the max file size limit for Code Search? By default, files larger than **1 MB** are excluded from search results. ### What programming languages are supported? Code Search supports almost all programming languages: Java, Python, Go, JavaScript, TypeScript, C#/C/C++, Swift, Objective-C, Kotlin, Ruby, Scala, Rust, Perl, Dart, Erlang, COBOL, Clojure, Lisp, Shell, Terraform, Lua, GraphQL, Thrift, Protobuf, YAML, JSON, Jsonnet, R, PHP, Elixir, Haskell, PowerShell, OCaml, CUDA, Pascal, Verilog, VHDL, Groovy, and Tcl. ### What deployment options are available with Code Search? Code Search supports the following deployment options: Kubernetes cluster, Amazon EKS or EC2, Google GKE, Microsoft, Azure AKS, Docker Compose, and Docker Compose in GCP. Read more about our [deployment docs here](/admin/deploy). ## Code Navigation ## Why are my results sometimes incorrect? If an index is not found for a particular file in a repository, Sourcegraph will fall back to search-based code navigation. You may occasionally see results from search-based code navigation even when you have uploaded an index. This can happen in the following scenarios: The line containing the symbol was created or edited between the nearest indexed commit and the commit being browsed. The Find references panel may include search-based results, but only after all of the precise results have been displayed. This ensures every symbol has useful code navigation. ## What languages are supported? Search-based code navigation supports 40 programming languages, including all of the most popular ones: Apex, Clojure, Cobol, C++, C#, CSS, Cuda, Dart, Elixir, Erlang, Go, GraphQL, Groovy, Haskell, Java, JavaScript, Jsonnet, Kotlin, Lisp, Lua, OCaml, Pascal, Perl, PHP, PowerShell, Protobuf, Python, R, Ruby, Rust, Scala, Shell, Starlark, Strato, Swift, Tcl, Thrift, TypeScript, Verilog, VHDL. ### Why does it sometimes time out? The [symbol search](/code-search/types/symbol) performance section describes query paths and performance. Consider using [Rockskip](/code-search/code-navigation/rockskip) if you're experiencing frequent timeouts.
# File Filtering in the Repository Comparison Page The repository comparison page provides powerful file filtering capabilities that allow you to focus on specific files in a comparison. The system supports multiple ways to specify which files you want to view when comparing branches, tags, or commits. ## Query parameter-based file filtering The comparison page supports three different query parameters to specify which files to include in the comparison: ### 1. Individual file paths You can specify individual files using either of these parameters: - `filePath=path/to/file.js` - Primary parameter for specifying files - `f=path/to/file.js` - Shorthand alternative Multiple files can be included by repeating the parameter: ```shell ?filePath=src/index.ts&filePath=src/components/Button.tsx ``` ### 2. Compressed file lists For comparisons with a large number of files, the system supports compressed file lists (newline-separated): - `compressedFileList=base64EncodedCompressedData` - Efficiently packs many file paths This parameter efficiently transmits large file paths using base64-encoded, gzip-compressed data. The compression allows hundreds or thousands of files to be included in a URL without exceeding length limits, which vary depending on the browser, HTTP server, and other services involved, like Cloudflare. ```typescript // Behind the scenes, the code decompresses file lists using: const decodedData = atoburl(compressedFileList) const compressedData = Uint8Array.from([...decodedData].map(char => char.charCodeAt(0))) const decompressedData = pako.inflate(compressedData, { to: 'string' }) ``` One way to create a list of files for the `compressedFileList` parameter is to use Python's built-in libraries to compress and encode using url-safe base64 encoding (smaller than base64-encoding, then url-encoding). ```shell python3 -c "import sys, zlib, base64; sys.stdout.write(base64.urlsafe_b64encode(zlib.compress(sys.stdin.buffer.read())).decode().rstrip('='))" < list.of.files > list.of.files.deflated.b64url ``` ### 3. Special focus mode You can focus on a single file using: - `focus=true&filePath=path/to/specific/file.js` - Show only this file in detail view ## File filtering UI components The comparison view provides several UI components to help you filter and navigate files: ### FileDiffPicker The FileDiffPicker component allows you to: - Search for files by name or path - Filter files by type/extension - Toggle between showing all files or only modified files - Sort files by different criteria (path, size of change, etc.) This component uses a dedicated file metadata query optimized for quick filtering. Results are displayed as you type. Through client-side filtering, the component can efficiently handle repositories with thousands of files. ### File navigation When viewing diffs, you can: - Click on any file in the sidebar to switch to that file - Use keyboard shortcuts to navigate between files - Toggle between expanded and collapsed views of files - Show or hide specific changes (additions, deletions, etc.) ### URL-based filtering Any filters you apply through the UI will update the URL with the appropriate query parameters. This means you can: 1. Share specific filtered views with others 2. Bookmark comparisons with specific file filters 3. Navigate back and forth between different filter states using browser history ## Implementation details The system makes strategic performance trade-offs to provide a smooth user experience: ```typescript /* * For customers with extremely large PRs with thousands of files, * we fetch all file paths in a single API call to enable client-side filtering. * * This eliminates numerous smaller API calls for server-side filtering * when users search in the FileDiffPicker. While requiring one large * initial API call, it significantly improves subsequent search performance. */ ``` The file filtering system uses a specialized file metadata query that is faster and lighter than the comprehensive file diffs query used to display actual code changes. ## Usage examples 1. View only JavaScript files: ```bash ?filePath=src/utils.js&filePath=src/components/App.js ``` 2. Focus on a single file: ```bash ?focus=true&filePath=src/components/Button.tsx ``` 3. Use a compressed file list for many files: ```bash ?compressedFileList=H4sIAAAAAAAAA2NgYGBg... ``` This flexible filtering system allows you to create customized views of repository comparisons, making reviewing changes in large projects easier. # Search Snippets

This page provides docs about how Search Snippets work with Sourcegraph.

Every project and team has a different set of repositories they commonly work with and queries they perform regularly. Custom search snippets enable users and organizations to quickly filter existing search results with search fragments matching those use cases. A search snippet is any valid query. For example, a search snippet that defines all repositories in the "example" organization would be `repo:^github\.com/example/`. After adding this snippet to your settings, it would appear in the search snippet panel in the search sidebar under a label of your choosing (as of v3.29). Search snippets are temporarily named search.scopes in site configuration files. ## Creating custom search snippets Custom search snippets can be specified at 3 different levels: - By site admins for all users: in the **Global settings** in the site admin area. - By organization admins for all organization members: in the organization profile **Settings** section - By users for themselves only: in the user profile **Settings** section You can configure search snippets by setting the `search.scopes` to a JSON array of `{name, value}` objects. The `value` of a search snippet can be any valid query and can include any [search token](/code-search/queries) (such as `repo:`, `file:`, etc.). For example, this JSON will create two search snippets: ```json { // ... "search.scopes": [ { "name": "Test code", "value": "file:(test|spec)" }, { "name": "Non-vendor code", "value": "-file:vendor/ -file:node_modules/" } ] // ... } ``` After editing and saving the configuration settings JSON in the profile page, your search snippets will be shown as suggested filters on search results pages. ![Snippets are in the search side panel](https://storage.googleapis.com/sourcegraph-assets/Docs/search-snippets.png)
# Search Subexpressions

This page provides docs about using Search Subexpressions in Sourcegraph's Code Search.

Search subexpressions combine groups of [filters](/code-search/queries#filters-all-searches) like `repo:` and [operators](/code-search/queries#boolean-operators) like `or`. Compared to [basic examples](/code-search/queries/examples), search subexpressions allow more sophisticated queries. Here are examples of how they can help you: 1. [Noncompliant spelling where case-sensitivity differs depending on the word](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+%28%28Github+case:yes%29+or+%28organisation+case:no%29%29&patternType=keyword). ```sgquery repo:sourcegraph ((Github case:yes) or (organisation case:no)) ``` The about code finds places to change the spelling of `Github` to `GitHub` (case-sensitivity matters) or change the spelling of `organisation` to `organization` (case-sensitivity does not matter). 2. [Search for either a file name or file contents scoped to the same repository](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+-file:html+%28file:router+or+newRouter%29&patternType=keyword). ```sgquery repo:sourcegraph -file:html (file:router or newRouter) ``` The above code finds both files containing the word `router` or file contents matching `newRouter` in the same repository, while excluding `html` files. Useful when exploring files or code that interact with a general term like `router`. 3. [Scope file content searches to particular files or repositories](https://sourcegraph.com/search?q=+repo:sourcegraph+%28%28file:schema%5C.graphql+hover%28...%29%29+or+%28file:codeintel%5C.go+%28Line+or+Character%29%29%29&patternType=structural) ```sgquery repo:sourcegraph ( (file:schema.graphql hover(...)) or (file:codeintel.go (Line or Character)) ) patterntype:structural ``` Combine matches of `hover(...)` in the `schema.graphql` file and matches of `Line` or `Character` in the `codeintel.go` file in the same repository. Useful for crafting queries that precisely match related fragments of a codebase to capture context and e.g., share with coworkers. The query is formatted for readability, it is valid as a single line query. 4. [Search across multiple repositories at multiple revisions](https://sourcegraph.com/search?q=%28repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24%40v3.22.0:v3.22.1+or+repo:%5Egithub%5C.com/sourcegraph/src-cli%24%403.22.0:3.22.1%29+file:CHANGELOG+campaigns&patternType=keyword). ```sgquery ( repo:^github\.com/sourcegraph/sourcegraph$@v3.22.0:v3.22.1 or repo:^github\.com/sourcegraph/src-cli$@3.22.0:3.22.1 ) file:CHANGELOG campaigns ``` Finds the word `campaigns` in `CHANGELOG` files for two repositories, `sourcegraph/sourcegraph ` or `sourcegraph/src-cli`, at specific revisions. Useful for searching across a larger scope of repositories at particular revisions. ## General tips for crafting queries with subexpressions ### Fully parenthesize subexpressions It's best practice to parenthesize queries to avoid confusion. For example, there are multiple ways to group the query, which is not fully parenthesized: ```sgquery repo:sourcegraph (Github case:yes) or (organisation case:no) ``` It could mean either of these: ```sgquery (repo:sourcegraph (Github case:yes)) or (organisation case:no) ``` ```sgquery repo:sourcegraph ((Github case:yes) or (organisation case:no)) ``` When parentheses are absent, we use the convention that operators divide sequences of terms that should be grouped together. That is: `file:main.c char c or (int i and int j)` generally means `(file:main.c char c) or (int i and int j)`. This convention means we would pick the following meaning of the original query, but it may not be what you intended: ```sgquery (repo:sourcegraph (Github case:yes)) or (organisation case:no) ``` Fully parenthesizing subexpressions makes it clear what the intent is, so that you can avoid relying on conventions that may not interpret the query the way you intended. ### Subexpression expansion If you're unsure whether a subexpression is valid, it may be useful to think in terms of how a subexpression expands to multiple independent queries. Expansion relies on a distributive property over `or`-expressions. For example: ```sgquery repo:sourcegraph ((Github case:yes) or (organisation case:no)) ``` This is equivalent to expanding the query by putting `repo:sourcegraph` inside each subexpression: ```sgquery (repo:sourcegraph Github case:yes) or (repo:sourcegraph organisation case:no) ``` This query is valid because each side of the `or` operator is a valid query. On the other hand, the following query is **invalid**: ```sgquery repo:sourcegraph case:yes (Github or (case:no organisation)) ``` Because after expanding it is equivalent to: ```sgquery (repo:sourcegraph case:yes Github) or (repo:sourcegraph case:yes case:no organisation) ``` After expanding, the right-hand side contains both `case:yes` and `case:no`. Since this subpart of the query is invalid, the original query is also invalid.
# Search Filters This page provides docs about how Search Filters UI works with Sourcegraph. Note: if you're looking for the filters search query syntax documentation you can find it in [search query syntax filters section](/code-search/queries#filters-all-searches) Search Query Syntax might not be that straightforward for new Sourcegraph users. It has a lot of query filters and operators, and it isn't easy sometimes to figure out how to use filters properly. With Search Filters UI, you can filter down your search results through the UI panel without manually modifying your query. ![Search filters are in the search result side panel](https://storage.googleapis.com/sourcegraph-assets/Docs/search-results-with-filters.png) The search filters UI panel tries to represent the most important filters in the search query syntax, and it should be straightforward enough to use without any additional knowledge about search query filters. However, here is a short list of facts to help you use them more effectively. ## 1. Search filters are derived from search results All filters that you can see in the panel are calculated from search matches that Sourcegraph found based on your query. This means that if your query doesn't return any symbols, diff or commit matches, there won't be a symbol, commit, diff related filters in the panel. ## 2. Default search filters If you try to run a search query without any `type:` filters, Sourcegraph will search results within three search types - `type:file` matches within file content - `type:repo` matches with repositories names - `type:path` matches with file names ![Snippets are in the search side panel](https://storage.googleapis.com/sourcegraph-assets/Docs/search-filters-by-type.png) If you want to search within some specific type, you can pick this type in the "By Type" filter section. ## 3. Search filters are not exhaustive By default, if your query has no `count:` filter Sourcegraph provides only 10k first results. This means that it is possible that you won't see some repositories in the "By repository" section if the search result limit is hit, even if this repository exists on your Sourcegraph's instance. To increase this limit and make sure your query and filters are exhaustive, include `count:all` in your query or select this option in the filter panel UI. ![Snippets are in the search side panel](https://storage.googleapis.com/sourcegraph-assets/Docs/search-filters-exhastive.png) ## 4. Move filters to the query Selected filters are preserved in the URL so you can easily share URl with other Sourcegraph users, and they will see selected filters. However, search query is still our first-class entity in Sourcegraph's ecosystem (code monitoring, batch changes, code insights). There is a way to convert your selected filters into your query by clicking the "Move filters to the query" button. ## 5. Filters are global `End query = Search Bar's query + Filters Panel UI query` The Filters panel works in a way that adds search query syntax filter operators to the original query in the search bar. This means that selected through filter UI panel filters are global, and this works just fine with simple queries, but if your query has complex parts and contains multiple operands and has `AND` and/or `OR` operators, we suggest using filters UI panel with caution since it may produce incorrect query. For example if your original query is `(test1 repo:sourcegraph/sourcegraph) OR (test2 repo:sourcegraph/about)` and you select `sourcegraph/sourcegraph` filter in the filters UI panel this filter will be applied for both operands `test1 repo:sourcegraph/sourcegraph` and `test2 repo:sourcegraph/about` # Search Contexts

This page provides docs about Search Contexts in Sourcegraph's Code Search.

Search Contexts help you search the code you care about on Sourcegraph. A search context represents a set of repositories at specific revisions on a Sourcegraph instance that will be targeted by search queries by default. Every search on Sourcegraph uses a search context. Search contexts can be defined with the contexts selector shown in the search input, or entered directly in a search query. ## Available contexts **Sourcegraph.com** supports a [set of predefined search contexts](https://sourcegraph.com/contexts?order=spec-asc&visible=17&owner=all) that include: - The global context, `context:global`, which includes all repositories on Sourcegraph.com. - Search contexts for various software communities like [CNCF](https://sourcegraph.com/search?q=context:CNCF), [crates.io](https://sourcegraph.com/search?q=context:crates.io), [JVM](https://sourcegraph.com/search?q=context:JVM), and more. If no search context is specified, `context:global` is used. **Private Sourcegraph instances** support custom search contexts: - Contexts owned by a user, such as `context:@username/context-name`, which can be private to the user or public to all users on the Sourcegraph instance. - Contexts at the global level, such as `context:example-context`, which can be private to site admins or public to all users on the Sourcegraph instance. - The global context, `context:global`, which includes all repositories on the Sourcegraph instance. ## Using search contexts The search contexts selector is shown in the search input. All search queries will target the currently selected search context. To change the current search context, press the contexts selector. All of your search contexts will be shown in the search contexts dropdown. Select or use the filter to narrow down to a specific search context. Selecting a different context will immediately re-run your current search query using the currently selected search context. Search contexts can also be used in the search query itself. Type `context:` to begin defining the context as part of the search query. When a context is defined in the search query itself, it overrides the context shown in the context selector. You can also search across multiple contexts at once using the `OR` [boolean operator](/code-search/queries#boolean-operators). For example: `(context:release1 OR context:release2 OR context:release3) someTerribleBug` ## Organizing search contexts To organize your search contexts better, you can use a specific context as your default and star any number of contexts. This affects what context is selected when loading Sourcegraph and how the list of contexts is sorted. ### Default search context #### For users Any authenticated user can use a search context as their default. To set a default, go to the search context management page, open the "..." menu for a context, and click on "Use as default". If the user doesn't have a default, `global` will be used. If a user ever loses access to their default search context (eg. the search context is made private), they will see a warning at the top of the search contexts dropdown menu list and `global` will be used. If a user's default search context is deleted, `global` will immediately be set as their default. The default search context is always selected when loading the Sourcegraph webapp. The one exception is when opening a link to a search query that does not contain a `context:` filter, in which case the `global` context will be used. #### For site admins Site admins can set a default search context for all users on the Sourcegraph instance. This helps teams improve onboarding and search quality by focusing searches on the most relevant parts of the codebase rather than the entire indexed set of repositories. An admin can set a default search context via: - Click the **More** button from the top menu of the Sourcegraph web app - Next, go to **Search Contexts** - For the existing context list, click on the **...** menu and select **[Site admin only] Set as default for all users** - Alternatively, you can create a new context and then set it for all users via the same option ![admin-level-default-context](https://storage.googleapis.com/sourcegraph-assets/Docs/admin-default-context.png) Here are a few considerations: - If a user already has a personal default search context set, it will not be overridden - The admin-set default will apply automatically if a user only uses the global context - Individual users can see the instance-wide default and override it with their own default if they choose ### Starred contexts Any authenticated user can star a search context. To star a context, click on the star icon in the search context management page. This will cause the context to appear near the top of their search contexts list. The `global` context cannot be starred. ### Sort order The order of search contexts in the search results dropdown menu list and in the search context management page is always the following: - The `global` context first - The user's default context, if set - All of the user's starred contexts - Any remaining contexts available ## Creating search contexts When search contexts are [enabled on your private Sourcegraph instance](/code-search/features#search-contexts), you can create your own search contexts. A search context consists of a name, description, and a set of repositories at one or many revisions. Contexts can be owned by a user, and can be private to the user or public to all users on the Sourcegraph instance. Contexts can also be at the global instance level, and can be private to site admins or public to all users on the Sourcegraph instance. ### Creating search contexts from header navigation - Go to **User menu > Search contexts** in the top navigation bar. - Press the **+ Create search context** button. - In the **Owner** field, choose whether you will own the context or if it will be global to the Sourcegraph instance. **Note**: At present, the owner of a search context cannot be changed after being created. - In the **Context name** field, type in a short, semantic name for the context. The name can be 32 characters max, and contain alphanumeric and `.` `_` `/` `-` characters. - Optionally, enter a **Description** for the context. Markdown is supported. - Choose the **Visibility** of this context. - Public contexts are available to everyone on the Sourcegraph instance. Note that private repositories will only be visible to users that have permission to view the repository via the code host. - Private contexts can only be viewed by their owner, or in the case being globally owned, by site admins. - In the **Repositories and revisions** configuration, define which repositories and revisions should be included in the search context. Press **Add repository** to quickly add a template to the configuration. - Define repositories with valid URLs. - Define revisions as strings in an array. To specify a default branch, use `"HEAD"`. For example: ```json [ { "repository": "github.com/sourcegraph/sourcegraph", "revisions": [ "3.15" ] }, { "repository": "github.com/sourcegraph/src-cli", "revisions": [ "3.11.2" ] } ] ``` - Press **Test configuration** to validate the repositories and revisions. - Press **Create search context** to finish creating your search context. You will be returned to the list of search contexts. Your new search context will appear in the search contexts selector in the search input, and can be [used immediately](#using-search-contexts). ## Query-based search contexts As of release 3.36, search contexts can be defined with a restricted search query as an alternative to a specific list of repositories and revisions. Allowed filters are: `repo`, `rev`, `file`, `lang`, `case`, `fork`, and `visibility`. `OR` and `AND` expressions are also allowed. Currently, repo built in predicates for example `repo:has.file`, `repo:has.content` etc, aren't currently supported in search contexts. If you're an admin, to enable this feature for all users set `experimentalFeatures.searchContextsQuery` to `true` in your global settings (for regular users, just use the normal settings menu). You'll then see a "Create context" button from the search results page and a "Query" input field in the search contexts form. If you want revisions specified in these query based search contexts to be indexed, set `experimentalFeatures.search.index.query.contexts` to `true` in site configuration. ### Creating search contexts from search results You can now create new search contexts right from the search results page. Once you've enabled query-based search contexts you'll see a Create context button above the search results. ## Managing search contexts with the API Learn how to [manage search contexts with the GraphQL API](/api/graphql/managing-search-contexts-with-api).
# Saved Searches

This page provides docs about how Saved Searches work with Sourcegraph.

Saved Searches lets you reuse and share search queries. You can create a saved search for anything, including diffs and commits across all branches of your repositories. Saved Searches functionality is available to both Free and Enterprise Code Search users. To access or create new Saved Searches in the Sourcegraph web app, click the **Tools > Saved Searches** in the top navigation bar. ![saved-searhces](https://storage.googleapis.com/sourcegraph-assets/Docs/saved-searches-2025.png) ## Creating saved searches To create a new saved search: - Go to the Saved Searches section and click the **New saved search** button - Fill out the description field and enter the search query - While writing the query syntax, ensure to include the `patternType:` field A `patternType:` filter is required in the query for all saved searches. `patternType` can be `keyword`, `standard`, `literal`, or `regexp`. You cannot create a saved search without defining the `patternType:` field. ![create-new-saved-search](https://storage.googleapis.com/sourcegraph-assets/Docs/create-new-saved-search-2025.png) Enable the checkbox for **Draft** if you don't want other users to use your saved search. This is useful for testing the query before sharing it with others. Once done, click the **Create saved search** button to be redirected to the Saved Searches page. Your saved search will appear with a `Secret` label, which means that only you can view and use it. To let others use your saved search, you need to transfer it to an organization and ask the site admin to make it public. In addition, you can also search within your saved searches and sort your saved searches by name, recently updated, and description. ![view-saved-searches](https://storage.googleapis.com/sourcegraph-assets/Docs/view-saved-searches-0824.png) ### Transfer ownership To transfer ownership of a saved search, click the **Edit** button next to it, click the **Transfer ownership** button, and select the organization to which you want to transfer the saved search. ![transfer-ownership](https://storage.googleapis.com/sourcegraph-assets/Docs/transfer-ownership-saved-search-2025.png) ## Example saved searches See the [search examples page](/code-search/queries/examples) for a useful list of searches to save.
# Symbol search Supported on [Enterprise Starter](/pricing/enterprise-starter) and [Enterprise](/pricing/enterprise) plans. Available via VS Code and JetBrains editor extensions and the Web. We use [Ctags](https://github.com/universal-ctags/ctags) to index the symbols of a repository on demand. These symbols are used to implement symbol search, matching declarations instead of plain text. ![symbol-search](https://storage.googleapis.com/sourcegraph-assets/Docs/Symbols.png) ## Symbol sidebar The extracted `ctags` symbols are also used for the symbol sidebar, which categorizes declarations by type (variable, function, interface, etc). Clicking on a symbol in the sidebar jumps you to the line where it is defined. ![symbol-sidebar](https://storage.googleapis.com/sourcegraph-assets/Docs/SymbolSidebar.png) ## Symbol search behavior and performance Here is the query path for symbol searches: - **Zoekt**: if [indexed search](/admin/search#indexed-search) is enabled and the search is for the tip commit of an indexed branch, then Zoekt will service the query and it should respond quickly. Zoekt indexes the default branch (usually `master` or `main`) and can be configured for [multi-branch indexing](/code-search/features#multi-branch-indexing-experimental). The high commit frequency of monorepos reduces the likelihood that Zoekt will be able to respond to symbol searches. Zoekt **eagerly** indexes by listening to repository updates, whereas the symbols service **lazily** indexes the commit being searched. - **Symbols service with Rockskip enabled**: if [Rockskip](/code-search/code-navigation/rockskip) is enabled, it'll search for symbols stored in Postgres. After initial indexing, queries should be resolved quickly. - **Symbols service with an index for the commit**: if the symbols service has already indexed this commit (i.e. someone has visited the commit before) then the query should be resolved quickly. Indexes are deleted in LRU fashion to remain under the configured maximum disk usage which [defaults to 100GB](/code-search/code-navigation/search_based_code_navigation#what-configuration-settings-can-i-apply). - **Symbols service with an index for a different commit**: if the symbols service has already indexed a **different** commit in the same repository, then it will make a copy of the previous index on disk then run [ctags](https://github.com/universal-ctags/ctags#readme) on the files that changed between the two commits and update the symbols in the new index. This process takes roughly 20 seconds on a monorepo with 40M LOC and 400K files. - **Symbols service without any indexes (cold start)**: if the symbols service has never seen this repository before, then it needs to run ctags on all symbols and construct the index from scratch. This process takes roughly 20 minutes on a monorepo with 40M LOC and 400K files. Once the symbols service has built an index for a commit, here's the query performance: - Exact matches `^foo$` are optimized to use an index - Prefix matches `^foo` are optimized to use an index - General regex queries `foo.*bar` are optimized to use an index when using [Rockskip](/code-search/code-navigation/rockskip), otherwise the default implementation needs to scan every symbol - Path filtering `file:^cmd/` helps narrow the search space Search-based code navigation uses exact matches `^foo$` and the symbols sidebar uses prefix matches on paths `file:^cmd/` to respond quickly. # Structural Search

Learn and understand about Sourcegraph's Structural Search and core functionality.

Changed in version 5.3. Structural search is disabled by default. To enable it, ask your site administrator to set experimentalFeatures.structuralSearch = "enabled" in site configuration. Structural search has performance limitations and is not actively developed. We recommend using regex search or a combination of [Search Jobs](./search-jobs.mdx) and custom scripts instead. Please reach out to [support@sourcegraph.com](mailto:support@sourcegraph.com) for guidance on alternative approaches. With structural search, you can match richer syntax patterns specifically in code and structured data formats like JSON. It can be awkward or difficult to match code blocks or nested expressions with regular expressions. To meet this challenge we've introduced a new and easier way to search code that operates more closely on a program's parse tree. We use [Comby syntax](https://comby.dev/docs/syntax-reference) for structural matching. Below you'll find examples and notes for this language-aware search functionality. ## Example The `fmt.Sprintf` function is a popular print function in Go. Here is a pattern that matches all the arguments in `fmt.Sprintf` calls in our code: ```go fmt.Sprintf(...) ``` The `...` part is special syntax that matches all characters inside the _balanced_ parentheses `(...)`. Let's look at two interesting variants of matches in our codebase. Here's one: ```go fmt.Sprintf("must be authenticated as an admin (%s)", isSiteAdminErr.Error()) ``` Note that to match this code, we didn't have to do any special thinking about handling the parentheses `(%s)` that happen _inside_ the first string argument, or the nested parentheses that form part of `Error()`. Unlike regular expressions, no "overmatching" can happen and the match will always respect balanced parentheses. With regular expressions, taking care to match the closing parentheses for this call could, in general, really complicate matters. Here is a second match: ```go fmt.Sprintf( "rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", pr.ToRef.Repository.Project.Key, pr.ToRef.Repository.Slug, pr.ID, ) ``` Here we didn't have to do any special thinking about matching contents that spread over multiple lines. The `...` syntax by default matches across newlines. Structural search supports balanced syntax like `()`, `[]`, and `{}` in a language-aware way. This allows it to match large, logical blocks or expressions without the limitations of typical line-based regular expression patterns. ## Syntax reference The syntax `...` above is an alias for a canonical syntax `:[hole]`, where `hole` is a descriptive identifier for the matched content. Identifiers are useful when expressing that matched content should be equal (see the [`return :[v.], :[v.]`](#match-equivalent-expressions) example below). Here is a table of additional syntax. | **Syntax** | **Alias** | **Description** | | ------------------------ | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `...` | `:[hole]`
`:[_]` | Match zero or more characters in a lazy fashion. When `:[hole]` is inside delimiters, as in `{:[h1], :[h2]}` or `(:[h])`, holes match within that group or code block, including newlines. | | `:[~regexp]` | `:[hole~regexp]` | Match an arbitrary [regular expression](https://golang.org/s/re2syntax) `regexp`. A descriptive identifier like `hole` is optional. Avoid regular expressions that match special syntax like `)` or `.*`, otherwise your pattern may fail to match balanced blocks. | | `:[[_]]`
`:[[hole]]` | `:[~\w+]`
`:[hole~\w+]` | Match one or more alphanumeric characters and underscore. | | `:[hole\n]` | `:[~.*\n]`
`:[hole~.*\n]` | Match zero or more characters up to a newline, including the newline. | | `:[ ]`
`:[ hole]` | `:[~[ \t]+]`
`:[hole~[ \t]+]` | Match only whitespace characters, excluding newlines. | | `:[hole.]` | `[_.]` | Match one or more alphanumeric characters and punctuation like `.`, `;`, and `-` that do not affect balanced syntax. Language dependent. | To match the string `...` literally, use regular expression patterns like `:[~[.]{3}]` or `:[~\.\.\.]`. ### Rules [Comby supports rules](https://comby.dev/docs/advanced-usage) to express equality constraints or pattern-based matching. Comby rules are not officially supported in Sourcegraph yet. We are in the process of making that happen and are taking care to address stable performance and usability. That said, you can explore rule functionality with an experimental `rule:` parameter. For example: [`buildSearchURLQuery(:[first], ...) rule:'where match :[first] { | " query: string" -> true }'`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+file:.ts+buildSearchURLQuery%28:%5Bfirst%5D%2C+...%29+rule:%27where+match+:%5Bfirst%5D+%7B+%7C+%22+query:+string%22+-%3E+true+%7D%27&patternType=structural) ## More examples Here are some additional examples. ### Match stringy data Taking the original `fmt.Sprintf(...)` example, let's modify the original pattern slightly to match only if the first argument is a string. We do this by adding string quotes around `...`. Adding quotes communicates _structural context_ and changes how the hole behaves: it will match the contents of a single string delimited by `"`. It _won't_ match multiple strings like `"foo", "bar"`. ```go fmt.Sprintf("...", ...) ``` Some matched examples are: ```go fmt.Sprintf("external service not found: %v", e.id) ``` ```go fmt.Sprintf("%s/campaigns/%s", externalURL, string(campaignID)) ``` Holes stop matching based on the first fragment of syntax that comes after it, similar to lazy regular expression matching. So, we could write: ```go fmt.Sprintf(:[first], :[second], ...) ``` To match all functions with three or more arguments, matching the `first` and `second` arguments based on the contextual position around the commas. ### Match equivalent expressions Using the same identifier in multiple holes adds a constraint that both of the matched values must be syntactically equal. So, the pattern: ```go return :[v.], :[v.] ``` Will match code where two identifier-like tokens in the `return` statement are the same. For example, `return true, true`, `return nil, nil`, or `return 0, 0`. ### Match JSON Structural search also works on structured data, like JSON. Use patterns to declaratively describe pieces of data to match. For example, the pattern: ```json "exclude": [...] ``` Matches all parts of a JSON document that have a member `"exclude"` where the value is an array of items. Visit our [structural search blog post](https://about.sourcegraph.com/blog/going-beyond-regular-expressions-with-structural-code-search) for more. ### Current functionality and configuration Structural search behaves differently to plain text search in key ways. We are continually improving the functionality of this new feature, so please note the following: - **Only indexed repos.** Structural search can currently only be performed on _indexed_ repositories. See [configuration](/admin/search) for more details if you host your own Sourcegraph installation. Our service hosted at [sourcegraph.com](https://sourcegraph.com/search) indexes approximately 200,000 of the most popular repositories on GitHub. Other repositories are currently unsupported. To see whether a repository on your instance is indexed, visit `https://.com/repo-org/repo-name/-/settings/index`. - **The `lang` filter is semantically significant.** Adding the `lang` [filter](/code-search/queries) informs the parser about language-specific syntax for comments, strings, and code. This makes structural search more accurate for that language. For example, `fmt.Sprintf(...) lang:go`. If `lang` is omitted, Sourcegraph will attempt a best-effort inference of the language based on matching file extensions or fall back to a generic structural matcher. - **Saved searches are not supported.** It is not currently possible to save structural searches. - **Matching blocks in indentation-sensitive languages.** It's not currently possible to match blocks of code that are indentation-sensitive. This is a feature planned for future work.
# Search Jobs Supported on [Enterprise](/pricing/enterprise) plans. Currently available via the Web app. Use Search Jobs to search code at scale for large-scale organizations. Search Jobs allows you to run search queries across your organization's codebase (all repositories, branches, and revisions) at scale. It enhances the existing Sourcegraph's search capabilities, enabling you to run searches without query timeouts or incomplete results. With Search Jobs, you can start a search, let it run in the background, and then download the results from the Search Jobs UI when it's done. Site administrators can **enable** or **disable** the Search Jobs feature, making it accessible to all users on the Sourcegraph instance. ## Using Search Jobs To use Search Jobs, you need to: - Run a search query from your Sourcegraph instance - Click the result menu below the search bar to see if your query is supported by Search Jobs ![run-query-for-search-jobs](https://storage.googleapis.com/sourcegraph-assets/Docs/search-jobs/search-jobs-create.png) - If your query is valid, click **Create a search job** to initiate the search job - You will be redirected to the "Search Jobs UI" page at `/search-jobs`, where you can view all your created search jobs. If you're a site admin, you can also view search jobs from other users on the instance - For each search job, you can download the results by clicking the **Download** button. The results are formatted as JSON lines, see [Search results format](#search-results-format) for more details. ![view-search-jobs](https://storage.googleapis.com/sourcegraph-assets/Docs/search-jobs/search-jobs-manage-with-frame.png) ## Search results format The downloaded results are formatted as [JSON lines](https://jsonlines.org). The JSON objects have the same format as the (event-type) matches served by the [Stream API](/api/stream_api#event-types). Here is an example of a JSON lines results file for a search job that ran the query `r:github.com/sourcegraph/sourcegraph handlePanic`. The search returned 4 results in 2 files: ```json lines {"type":"content","path":"cmd/symbols/shared/main.go","repositoryID":399,"repository":"github.com/sourcegraph/sourcegraph","branches":["HEAD"],"commit":"9b47c6430cc3c9cb16695b4fa0a1a277cbbdb327","hunks":null,"chunkMatches":[{"content":"func handlePanic(logger log.Logger, next http.Handler) http.Handler {","contentStart":{"offset":4526,"line":129,"column":0},"ranges":[{"start":{"offset":4531,"line":129,"column":5},"end":{"offset":4542,"line":129,"column":16}}]},{"content":"\thandler = handlePanic(logger, handler)","contentStart":{"offset":3570,"line":98,"column":0},"ranges":[{"start":{"offset":3581,"line":98,"column":11},"end":{"offset":3592,"line":98,"column":22}}]}],"language":"Go"} {"type":"content","path":"cmd/embeddings/shared/main.go","repositoryID":399,"repository":"github.com/sourcegraph/sourcegraph","branches":["HEAD"],"commit":"9b47c6430cc3c9cb16695b4fa0a1a277cbbdb327","hunks":null,"chunkMatches":[{"content":"func handlePanic(logger log.Logger, next http.Handler) http.Handler {","contentStart":{"offset":6585,"line":190,"column":0},"ranges":[{"start":{"offset":6590,"line":190,"column":5},"end":{"offset":6601,"line":190,"column":16}}]},{"content":"\thandler = handlePanic(logger, handler)","contentStart":{"offset":2844,"line":81,"column":0},"ranges":[{"start":{"offset":2855,"line":81,"column":11},"end":{"offset":2866,"line":81,"column":22}}]}],"language":"Go"} ``` ## Configure Search Jobs Search Jobs requires an object storage to store the results of your search jobs. By default, Search Jobs stores results using our bundled `blobstore` service. If the `blobstore` service is deployed, and you want to use it to store results from Search Jobs, you don't need to configure anything. To use a third party managed object storage service, see instructions in [externalizing object storage](../../admin/external_services/object_storage#search-job-results). ### Environment Variables You can configure Search Jobs behavior using the following environment variables on the worker service: | Environment Variable | Default Value | Description | | --- | --- | --- | | `SRC_SEARCH_JOB_WORKER_INTERVAL` | `1s` | Controls how frequently the system checks for new search jobs to process. You probably don't need to configure this.| | `SRC_SEARCH_JOB_MAXIMUM_RUNTIME_PER_JOB` | `5h` | Sets a maximum time limit for how long a search job can run per repository-revision pair. Note that a search job is run as a collection of many smaller searches, each targeting a single revision of a repository. This ENV targets the maximum runtime of individual searches.| | `SRC_SEARCH_JOB_NUM_HANDLERS` | `5` | Adjusts how many searches can run in parallel. A value of 5 (default) means that 5 searches, each targeting a different repository-revision pair, can run in parallel. More handlers put more pressure on the backend, particularly on Searcher and Zoekt. Make sure you have sufficient resources before increasing the number of handlers. | ## Supported result types Search jobs supports the following result types: - `file` - `path` - `diff` - `commit` The following result types are not supported: - `symbol` - `repo` ## Limitations The following elements of our query language are not supported: - file predicates, such as `file:has.content`, `file:has.owner`, `file:has.contributor`, `file:contains.content` - catch-all `.*` regexp search - Multiple `rev` filters - Queries with `index: filter` Alternatively, the search bar supports the `count:all` operator which increases result limits and timeouts. This works well if the search completes within a few minutes and the number of results is less than the configured display limit. For longer running searches and searches with huge result sets, Search Jobs is the better choice. ## Disable Search Jobs To disable Search Jobs, set `DISABLE_SEARCH_JOBS=true` in your frontend and worker services. ## FAQ ### How can I run a search job against all branches and all repositories? You can use a combination of the `repo:` and `rev:` filter to search across all branches and repositories. For example, `r: rev:*/refs/heads/* foo` will search for `foo` across all branches and all repositories. See the [search syntax documentation](/code-search/queries#repository-revisions) for more details. ### Is there a time limit for a search job? We break down a search job into smaller tasks and run them in parallel. Each task is limited to a single revision and a single repository. This allows us to run searches across large codebases. The chance of hitting timeouts is very low, but it's not impossible. If you do hit a timeout, please reach out to support and we will help you troubleshoot the issue. As an immediate measure, you can break down your search query into smaller queries and run them separately. ### A search job failed, how can I find out what went wrong? We mark a search job as failed if any of the tasks fail. You can access the log of a search job from the search jobs UI. The log contains one line per task. If a task fails, the log will contain the error message. # Fuzzy Finder

Learn and understand about Sourcegraph's Fuzzy Search and core functionality.

Use the fuzzy finder to quickly navigate to a repository, symbol, or file. To open the fuzzy finder, press `Cmd+K` (macOS) or `Ctrl+K` (Linux/Windows) from any page. Use the dedicated Repos, Symbols, and Files tabs to search only for a repository, symbol, or file. Each tab has a dedicated shortcut: - **Repos**: Cmd+I (macOS), Ctrl+K (Linux/Windows) - **Symbols**: Cmd+O (macOS), Cmd+Shift+O (macOS Safari), Ctrl+O (Linux/Windows) - **Files**: Cmd+P (macOS), Ctrl+P (Linux/Windows) Fuzzy finder Use the **Searching everywhere** or **Searching in this repo** filter to determine whether to search for results only in the active repository or globally. Fuzzy finder
# Deep Search

Learn more about Sourcegraph's agentic Code Search tool Deep Search.

New in version 6.4. Deep Search is currently in research preview for Enterprise customers with access to Cody and Code Search. Because Deep Search is in research preview, it might change significantly in the future as we make improvements and adjust to user feedback. Please reach out to your Sourcegraph account team to request access. Deep Search is an agentic code search tool. It receives a natural language question about a codebase, performs an in-depth search, and returns a detailed answer. The user can also ask follow-up questions to improve the answer further. Under the hood, Deep Search is an AI agent that uses various tools to generate its answer. These tools include multiple modes of Sourcegraph's Code Search and Code Navigation tools. Using tools in an agentic loop enables Deep Search to iteratively refine its understanding of the question and codebase, searching until it is confident in its answer. Deep Search displays a list of sources used to generate the answer. The sources are the various types of searches it performs and the files it reads. The answer is formatted in markdown. If prompted to do so, Deep Search can also generate diagrams as part of its answer. ## Best practices - Give the agent a starting point for the search: Mention relevant repositories, files, directories, or symbol names. The more specific you are, the faster the search will be. - Provide reasonably scoped questions. The agent will perform much better if it does not have to read the entire codebase at once. - Check the list of sources. This is extremely useful for debugging and understanding where the answer came from. If something is missing, ask a follow-up question and mention the missing source. ### Examples of prompts - Find examples of logger usage and show examples of the different types of logging we use. - I want to know when the indexing queue functionality was last changed in `sourcegraph/zoekt`. Show me the last few commit diffs touching this code and explain the changes. - Look at the GraphQL APIs available in `sourcegraph/sourcegraph`. Are any of them unused? The client code is in `sourcegraph/cody`. - Which tools do we use in our build processes defined in `BUILD.bazel` files? - Generate a request flow diagram for `src/backend`. Mark the auth and rate limit points. ## Enabling Deep Search Deep Search can only be used on Sourcegraph instances with Code Search and Cody licenses. Deep Search is disabled by default. To enable it, ask your site administrator to set `experimentalFeatures.deepSearch.enabled = "true"` in your site configuration. For optimal performance, Deep Search is specialized to only use one model. Currently, Deep Search only supports Claude Sonnet 4. ### Configuring Deep Search on Amazon Bedrock or GCP Vertex Include configuration for Claude Sonnet 4 in [modelOverrides](/cody/enterprise/model-configuration#model-overrides) in your site configuration. For more information on configuring models, refer to [Model Configuration](/cody/enterprise/model-configuration). Examples for Sonnet 4 configuration inside `modelOverrides`: Amazon Bedrock: ```json { "modelRef": "aws-bedrock::v1::claude-sonnet-4", "modelName": "us.anthropic.claude-sonnet-4-20250514-v1:0", "displayName": "Claude Sonnet 4 (AWS Bedrock)", "capabilities": [ "chat", "tools", ], "category": "balanced", "status": "stable", "contextWindow": { "maxInputTokens": 200000, "maxOutputTokens": 32000, } }, ``` GCP Vertex: ```json { "modelRef": "google-anthropic::v2::claude-sonnet-4", "modelName": "claude-sonnet-4@20250514", "displayName": "Claude Sonnet 4 (GCP Vertex)", "capabilities": [ "chat", "tools", ], "category": "balanced", "status": "stable", "contextWindow": { "maxInputTokens": 200000, "maxOutputTokens": 32000, } }, ``` Then, configure Deep Search to use this model in `experimentalFeatures`: Amazon Bedrock: ```json "experimentalFeatures": { "deepSearch.enabled": true, "deepSearch.model": "aws-bedrock::v1::claude-sonnet-4" }, ``` GCP Vertex: ```json "experimentalFeatures": { "deepSearch.enabled": true, "deepSearch.model": "google-anthropic::v2::claude-sonnet-4" }, ```
# Search Query Language Reference

This page provides a visual breakdown of the Search Query Language with some examples to get you started.

It is complementary to our [Search Query Syntax](/code-search/queries) and illustrates syntax using railroad diagrams instead of tables. ## How to read railroad diagrams? Follow the lines in these railroad diagrams to see how pieces of syntax combine. When a line splits, it means there are multiple options available. When it is possible to repeat a previous syntax, the split line will loop back on itself like this: ![repeatable](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repeatable-lang.png) ## Basic query ![basic-query](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/basic-query.png) At a basic level, a query consists of [search patterns](#search-pattern) and [parameters](#parameter). Typical queries contain one or more space-separated search patterns that describe what to search, and parameters refine searches by filtering results or changing search behavior. For example, - [`repo:github.com/sourcegraph/sourcegraph file:schema.graphql The result`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+file:schema.graphql+%22The+result%22&patternType=keyword) ## Expression ![expression](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/expression.png) Build query expressions by combining [basic queries](#basic-query) and operators like `AND` or `OR`. Group expressions with parentheses to build more complex expressions. If there are no balanced parentheses, `AND` operators bind tighter, so `foo or bar and baz` means `foo or (bar and baz)`. You may also use lowercase `and` or `or`. For example, - [`repo:github.com/sourcegraph/sourcegraph rtr AND newRouter`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rtr+AND+newRouter&patternType=keyword) ## Search pattern ![search-pattern](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/search-pattern.png) A pattern to search. By default, the pattern is searched literally. The kind of search may be toggled to change how a pattern matches: Perform a [regular expression search](/code-search/queries#regular-expression-search). We support [RE2 syntax](https://golang.org/s/re2syntax). Quoting a pattern will perform a literal search. For example, - [`foo.*bar.*baz`](https://sourcegraph.com/search?q=foo.*bar.*baz&patternType=regexp) - [`"foo bar"`](https://sourcegraph.com/search?q=%22foo+bar%22&patternType=regexp) ## Parameter ![parameter](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/parameter.png) Search parameters allow you to filter search results or modify search behavior. ### Repo ![repo](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo.png) Search repositories that match the regular expression. A `-` before `repo` excludes the repository. By default, the repository will be searched at the `HEAD` commit of the default branch. You can optionally change the [revision](#revision). For example, - [`repo:gorilla/mux testroute`](https://sourcegraph.com/search?q=repo:gorilla/mux+testroute&patternType=regexp) - [`-repo:gorilla/mux testroute`](https://sourcegraph.com/search?q=-repo:gorilla/mux+testroute&patternType=regexp) ### Revision ![revision](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/revision.png) Search a repository at a given revision, for example, a branch name, commit hash, or Git tag. For example, - [`repo:^github\.com/sourcegraph/sourcegraph$@75ba004 get_embeddings`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24%4075ba004+get_embeddings+&patternType=keyword) - [`repo:^github\.com/sourcegraph/sourcegraph$ rev:v5.0.0 get_embeddings`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:v5.0.0+get_embeddings&patternType=keyword) You can search multiple revisions by separating the revisions with `:`. Specify `HEAD` for the default branch. For example, - [`repo:^github\.com/sourcegraph/sourcegraph$ rev:v4.5.0:v5.0.0 disableNonCriticalTelemetry`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:v4.5.0:v5.0.0+disableNonCriticalTelemetry&patternType=keyword) - [`repo:^github\.com/sourcegraph/sourcegraph$@v4.5.0:v5.0.0 disableNonCriticalTelemetry`](https://sourcegraph.com/search?q=context%3Aglobal+repo%3A%5Egithub%5C.com%2Fsourcegraph%2Fsourcegraph%24%40v4.5.0%3Av5.0.0+disableNonCriticalTelemetry&patternType=keyword) ### Revision at time This feature is only supported for Sourcegraph versions 5.4 or more. Search a repository at a given time. Optionally, a second parameter can be used to specify a revision which will be used as the starting point of the search. For example, - [`repo:^github\.com/sourcegraph/sourcegraph$ rev:at.time(2 years ago) handbook`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:at.time%282+years+ago%29+handbook&patternType=keyword&sm=0) - [`repo:^github\.com/sourcegraph/sourcegraph$ rev:at.time(2021-01-30, v5.0.0) popular`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:at.time%282021-01-30%2C+v5.0.0%29+popular&patternType=keyword&sm=0) ### File ![file](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/files.png) Search files whose full path matches the regular expression. A `-` before `file` excludes the file from being searched. For example, - [`file:\.js$ httptest`](https://sourcegraph.com/search?q=file:%5C.js%24+httptest&patternType=regexp) - [`file:\.js$ -file:test http`](https://sourcegraph.com/search?q=file:%5C.js%24+-file:test+http&patternType=regexp) ### Language ![language](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/language.png) Only search files in the specified programming language, like `typescript` or `python`. For example, - [`lang:typescript encoding`](https://sourcegraph.com/search?q=lang:typescript+encoding&patternType=regexp) #### Content-based language detection (Beta) Language filters work by checking the file name and extension. They can behave unexpectedly when a language's extension is ambiguous: for example `lang:Objective-C` may also match Matlab files, since both languages use the `.m` extension. If this is an issue, you can enable the feature flag `search-content-based-lang-detection`. When enabled, Sourcegraph more accurately detects a file's language by checking the file contents in addition to the name and extension. ### Content ![content](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/content.png) Set the search pattern to search using a dedicated parameter. Useful, for example, when searching literally for a string like `repo:my-repo` that may conflict with the syntax of parameters in this Sourcegraph language. For example, - [`repo:sourcegraph content:"repo:sourcegraph"`](https://sourcegraph.com/search?q=repo:sourcegraph+content:%22repo:sourcegraph%22&patternType=keyword) ### Select ![select](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/select.png) Selects the specified result type from the set of search results. If a query produces results that aren't of the selected type, the results will be converted to the selected type. For example, the query `file:package.json lodash` will return content matches for `lodash` in `package.json` files. If `select:repo` is added, the containing repository will be selected and the _repositories_ that contain `package.json` files that contain the term `lodash` will be returned. All selected results are deduplicated, so if there are multiple content matches in a repository, `select:repo` will still only return unique results. A query like `type:commit example select:symbol` will return no results because commits have no associated symbol and cannot be converted to that type. For example, - [`fmt.Errorf select:repo`](https://sourcegraph.com/search?q=fmt.Errorf+select:repo&patternType=keyword) - [`zoektSearch select:file`](https://sourcegraph.com/search?q=zoektSearch+select:file&patternType=keyword) #### Symbol kind ![symbol-kind](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/symbol-kind.png) Select a specific kind of symbol. For example `type:symbol select:symbol.function zoektSearch` will only return functions that contain the literal `zoektSearch`. For example, - [`type:symbol zoektSearch select:symbol.function`](https://sourcegraph.com/search?q=type:symbol+zoektSearch+select:symbol.function&patternType=keyword) #### Modified lines ![modified-lines](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/modified-lines.png) When searching commit diffs, select only diffs where the pattern matches on `added` or `removed` lines. For example, search for recent commits that removed `TODO`s in your code. If any line exists that satisfies the condition, the entire diff is included in the result set. `type:diff` must be specified in the query. For example, - [`repo:^github\.com/sourcegraph/sourcegraph$ type:diff TODO select:commit.diff.removed`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+type:diff+TODO+select:commit.diff.removed+&patternType=keyword) #### File kind ![file-kind](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/file-kind.png) Select only directory paths of file results with `select:file.directory`. This is useful for discovering the directory paths that specify a `package.json` file, for example. `select:file.path` returns the full path for the file and is equivalent to `select:file`. It exists as a fully-qualified alternative. For example, - [`file:package\.json select:file.directory`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+file:package%5C.json+select:file.directory&patternType=keyword) #### File owners ![file-owners](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/file-owners.png) Select owners associated with the results of a query. For example, `lang:TypeScript select:file.owners` displays owners of all TypeScript files. ### Type ![type](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/type.png) Set whether the search pattern should perform a search of a certain type. Notable search types are symbol, commit, and diff. For example, - [`type:symbol path`](https://sourcegraph.com/search?q=type:symbol+path)  - [`type:commit author:nick`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph%24+type:commit+author:nick&patternType=regexp) ### Case ![case](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/case.png) Set whether the search pattern should be treated case-sensitively. This is synonymous with the **toggle** button. For example, - [`OPEN_FILE case:yes`](https://sourcegraph.com/search?q=OPEN_FILE+case:yes) ### Fork ![fork](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/fork.png) Set to `yes` if repository forks should be included or `only` if only forks should be searched. Repository forks are excluded by default. For example, - [`fork:yes repo:sourcegraph`](https://sourcegraph.com/search?q=fork:yes+repo:sourcegraph&patternType=regexp) ### Archived ![archived](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/archived.png) Set to `yes` if archived repositories should be included or `only` if only archives should be searched. Archived repositories are excluded by default. For example, - [`archived:only repo:sourcegraph`](https://sourcegraph.com/search?q=archived:only+repo:sourcegraph&patternType=regexp) ### Count ![count](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/count.png) Retrieve N results. By default, Sourcegraph stops searching early and returns if it finds a full page of results. This is desirable for most interactive searches. To wait for all results, use **count:all**. For example, - [`count:1000 function`](https://sourcegraph.com/search?q=count:1000+repo:sourcegraph/sourcegraph%24+function&patternType=regexp) - [`count:all err`](https://sourcegraph.com/search?q=repo:github.com/sourcegraph/sourcegraph+err+count:all&patternType=keyword) ### Timeout ![timeout](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/timeout.png) Set a search timeout. The time value is a string like 10s or 100ms, which is parsed by the Go time package's [ParseDuration](https://golang.org/pkg/time/#ParseDuration). By default, the timeout is set to 10 seconds, and the search will optimize for returning results as soon as possible. The timeout value cannot be set to longer than 1 minute. For example, - [`timeout:15s count:10000 func`](https://sourcegraph.com/search?q=repo:%5Egithub.com/sourcegraph/+timeout:15s+func+count:10000) sets a longer timeout for a search that contains a lot of results. Sourcegraph admins can increase the maximum timeout of 1 minute through [site configuration](/admin/search#maximum-timeout). The maximum timeout can also depend on the HTTP load balancer in front of Sourcegraph (nginx/ELB/Cloudflare/etc). Sourcegraph admins may need to increase timeouts for Sourcegraph paths, particularly the `.api/search/stream` path. This uses [SSE](https://en.wikipedia.org/wiki/Server-sent_events), so your reverse proxy may have specific support for these requests. ### Visibility ![visibility](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/visibility.png) Filter results to only public or private repositories. The default is to include both private and public repositories. For example, - [`type:repo visibility:public`](https://sourcegraph.com/search?q=type:repo+visibility:public&patternType=regexp) ### Pattern type ![pattern-type](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/pattern-type.png) Set whether the pattern should run a [keyword search](/code-search/queries#keyword-search-default) or [regular expression search](/code-search/queries#regular-expression-search). This parameter is available as a command-line and accessibility option and is synonymous with the visual [search pattern](#search-pattern) toggles. ## Built-in repo predicate ![builtin-repo-predicate](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/builtin-repo-predicate.png) ### Repo has meta Tagging repositories with key-value pairs is GA as of 5.1.0, but can be disabled by creating the feature flag `repository-metadata` and setting it to `false`. Add metadata by [following the instructions](/admin/repo/metadata). ![repo-has-meta](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo-has-meta.png) Search only inside repositories that are associated with the provided key-value pair, key, or tag. Keys and value patterns can be literal strings which match the key and value exactly, or they can be slash-delimited regex patterns. For example, - [`repo:has.meta(team:sourcegraph)`](https://sourcegraph.com/search?q=context:global+repo:has.meta%28team:sourcegraph%29&patternType=regexp&sm=1&groupBy=repo) - [`repo:has.meta(team:/[source]{5}graph/)`](https://sourcegraph.com/search?q=context:global+repo:has.meta%28team:/%5Bsource%5D%7B5%7Dgraph/%29&patternType=regexp&sm=1) - [`repo:has.meta(language)`](https://sourcegraph.com/search?q=context%3Aglobal+repo%3Ahas.meta%28language%29&patternType=regexp&sm=1&groupBy=repo) ### Repo has file and content ![repo-has-file-and-content](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo-has-file-and-content.png) Search only inside repositories that contain a file matching the `path:` with `content:` filters. For example, - [`repo:has.file(path:CHANGELOG content:fix)`](https://sourcegraph.com/search?q=context:global+repo:github%5C.com/sourcegraph/.*+repo:has.file%28path:CHANGELOG+content:fix%29&patternType=keyword) `repo:contains.file(...)` is an alias for `repo:has.file(...)` and behaves identically. ### Repo has path ![repo-has-path](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo-has-path.png) Search only inside repositories that contain a file path matching the regular expression. For example, - [`repo:has.path(README)`](https://sourcegraph.com/search?q=context:global+repo:github%5C.com/sourcegraph/.*+repo:has.path%28README%29&patternType=keyword) `repo:contains.path(...)` is an alias for `repo:has.path(..)` and behaves identically. ### Repo has content ![repo-has-content](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo-has-content.png) Search only inside repositories that contain file content matching the regular expression. For example, - [`repo:github\.com/sourcegraph/.*$ repo:has.content(TODO)`](https://sourcegraph.com/search?q=repo:github%5C.com/sourcegraph/.*%24+repo:has.content%28TODO%29&patternType=keyword) `repo:contains.content(...)` is an alias for `repo:has.content(...)` and behaves identically. ### Repo has topic ![repo-has-topic](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo-has-topic.png) Search only inside repositories that have the given GitHub/GitLab topic. For example, - [`repo:has.topic(code-search)`](https://sourcegraph.com/search?q=context%3Aglobal+repo%3Ahas.topic%28code-search%29&patternType=keyword&groupBy=repo) Topic search is currently only supported for GitHub and GitLab repos. ### Repo has commit after ![repo-has-commit-after](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo-has-commit-after.png) Search only inside repositories that contain a commit after some specified time. See [git date formats](https://github.com/git/git/blob/master/Documentation/date-formats.txt) for accepted formats. Use this to filter out stale repositories that don’t contain commits past the specified time frame. This parameter is experimental. For example, - [`repo:has.commit.after(1 month ago)`](https://sourcegraph.com/search?q=context:global+repo:.*sourcegraph.*+repo:has.commit.after%281+month+ago%29&patternType=keyword) `repo:contains.commit.after(...)` is an alias for `repo:has.commit.after(...)` and behaves identically. ### Repo has description ![repo-has-description](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/repo-has-description.png) Search only inside repositories having a description matching the given regular expression. For example, - [`repo:has.description(go package)`](https://sourcegraph.com/search?q=context:global+repo:has.description%28go.*package%29+&patternType=keyword) ## Built-in file predicate ![builtin-file-predicate](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/builtin-file-predicate.png) ### File has content ![file-has-content](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/file-has-content.png) Search only inside files that contain content matching the provided regexp pattern. For example, - [`file:has.content(test)`](https://sourcegraph.com/search?q=context:global+repo:github%5C.com/sourcegraph/.*+file:has.content%28test%29&patternType=keyword) `file:contains.content(...)` is an alias for `file:has.content(...)` and behaves identically. ### File has owner ![file-has-owner](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/file-has-owner.png) Search only inside files that have an owner associated matching given string. When no parameter is supplied, the predicate only includes files with any owner assigned to them: `file:has.owner()` will include files with any owner assigned and `-file:has.owner()` will only include files without an owner. ### File has contributor ![file-has-contributor](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/file-has-contributor.png) Search only inside files that have a contributor whose name or email matches the provided regex pattern. ## Regular expression ![regular-expression](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/regular-expression.png) A string that is interpreted as a [RE2](https://golang.org/s/re2syntax) regular expression. ## String ![string](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/string.png) An unquoted string is any contiguous sequence of characters not containing whitespace. ## Quoted string ![quoted-string](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/quoted-string.png) Any string, including whitespace, may be quoted with single `'` or double `"` quotes. Quotes can be escaped with `\`. Literal `\` characters will need to be escaped, for example, `\\`. ## Commit parameter ![commit-parameter](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/commit-parameter.png) Set parameters that apply only to commit and diff searches. ### Author ![author](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/author.png) Include commits or diffs that are authored by the user. ### Before ![before](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/before.png) Include results having a commit date before the specified time frame. Times are interpreted as UTC by default. Many forms are accepted for the argument, such as: - `november 1 2019` - `1 november 2019` - `2019.11.1` - `11/1/2019` - `01.11.2019` - `Thu, 07 Apr 2005 22:13:13 +0200` - `2005-04-07` - `2005-04-07T22:13:13` - `2005-04-07T22:13:13+07:00` - `yesterday` - `5 days ago` - `20 minutes ago` - `2 weeks ago` - `3:00` - `3pm` - `1632782809` - `1632782809 -0600` For example, - [`before:"last thursday"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph%24+type:diff+author:nick+before:%22last+thursday%22&patternType=regexp) - [`before:"november 1 2019"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick+before:%22november+1+2019%22) ### After ![after](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/after.png) Include results having a commit date after the specified time frame. Times are interpreted as UTC by default. Many forms are accepted for the argument, such as: - `november 1 2019` - `1 november 2019` - `2019.11.1` - `11/1/2019` - `01.11.2019` - `Thu, 07 Apr 2005 22:13:13 +0200` - `2005-04-07` - `2005-04-07T22:13:13` - `2005-04-07T22:13:13+07:00` - `yesterday` - `5 days ago` - `20 minutes ago` - `2 weeks ago` - `3:00` - `3pm` - `1632782809` - `1632782809 -0600` For example, - [`after:"6 weeks ago"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph%24+type:diff+after:%226+weeks+ago%22&patternType=keyword) - [`after:"november 1 2019"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick+after:%22november+1+2019%22) ### Message ![message](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/message.png) Include results having a commit message containing the string. For example, - [`type:commit message:"testing"`](https://sourcegraph.com/search?q=type:commit+message:%22testing%22+repo:sourcegraph/sourcegraph%24+&patternType=regexp) ## Whitespace ![whitespace](https://storage.googleapis.com/sourcegraph-assets/Docs/CS-LANG/whitespace.png)
# Search Query Syntax

This page describes the query syntax for Code Search.

A typical search consists of two parts: * A [search pattern](#search-patterns) containing the terms you want to search, for example `println` * [Search filters](#filters-all-searches) that scope the search to certain repositories, languages, etc., for example `lang:java` For a graphical view of Sourcegraph's query syntax, see the [search language reference](/code-search/queries/language). ## Search patterns This section documents the search pattern syntax in Sourcegraph. To match file content, you need to specify a search pattern. Search patterns are optional when searching [commits](#filters-diff-and-commit-searches-only), [filenames](#filename-search), or [repository names](#repository-name-search). ### Keyword search (default) Keyword search matches individual terms anywhere in the document or the filename. Use `"..."` to match phrases exactly. Specify regular expressions inside `/.../`. | Search pattern syntax | Description | | ------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------- | | [`foo bar`](https://sourcegraph.com/search?q=foo+bar&patternType=keyword) | Match documents containing both `foo` and `bar` anywhere in the document. | | [`"foo bar"`](https://sourcegraph.com/search?q=%22foo+bar%22&patternType=keyword) | Match the string `foo bar` exactly. The space between the terms is interpreted literally. The quotes are not matched. | | [`"foo \"bar\""`](https://sourcegraph.com/search?q=context:global+%22foo+%5C%22bar%5C%22%22&patternType=keyword) | Match the string `foo "bar"` exactly. | | [`/foo.*bar/`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+/foo.*bar/&patternType=keyword) | Match the **regular expression** `foo.*bar`. We support [RE2 syntax](https://golang.org/s/re2syntax). | | [`foo OR bar`](https://sourcegraph.com/search?q=context:global+foo+OR+bar&patternType=keyword) | Match documents containing `foo` _or_ `bar` anywhere in the document. | Matching is case-insensitive by default (toggle the `Aa` button to change). In version 5.4, `keyword` replaced `standard` as the new default pattern type. ### Regular expression search Clicking the (.*) toggle interprets all search patterns as regular expressions. In [Keyword search](#keyword-search-default), use `/.../` to perform regular expression searches. Use this mode if you prefer writing out regular expressions without enclosing them. In this mode spaces between patterns mean **match anything**. Patterns inside quotes mean **match exactly**. | **Search Syntax** | **Description** | | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [`foo bar`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+foo+bar&patternType=regexp) | Search for the regexp `foo(.*?)bar`. Spaces between non-whitespace strings becomes `.*?` to create a fuzzy search | | [`foo\ bar`](https://sourcegraph.com/search?q=foo%5C+bar&patternType=regexp) or [`/foo bar/`](https://sourcegraph.com/search?q=/foo+bar/&patternType=regexp) | Search for the regexp `foo bar`. The `\` escapes the space and treats the space as part of the pattern. Use `/.../` to avoid escaping spaces | | [`foo\nbar`](https://sourcegraph.com/search?q=foo%5Cnbar&patternType=regexp) | Perform a multiline regexp search. `\n` is interpreted as a newline | | [`"foo bar"`](https://sourcegraph.com/search?q=%22foo+bar%22&patternType=regexp) | Match the string `foo bar` exactly. The space between the terms is interpreted literally. The quotes are not matched. | We support [RE2 syntax](https://golang.org/s/re2syntax). Matching is case-insensitive by default (toggle the `Aa` button to change). ## Filters (all searches) The following filters can be used on all searches (using [RE2 syntax](https://golang.org/s/re2syntax) any place a regex is accepted): | **Filters** | **Description** | **Examples** | |----------|----------|----------| | **repo:regexp-pattern**
**repo:regexp-pattern@rev**
**repo:regexp-pattern rev:rev**
_alias: r_ | Include results from repos whose path matches a specified regexp-pattern. The repo path is `github.com/myteam/abc` or `code.example.com/xyz` depending on your repo host. If the regexp ends in `@rev`, search that revision instead of the default `main` branch. `repo:regexp-pattern@rev` is equivalent to `repo:regexp-pattern rev:rev` | [`repo:gorilla/mux testroute`](https://sourcegraph.com/search?q=repo:gorilla/mux+testroute)
[`repo:^github\.com/sourcegraph/sourcegraph$@v3.14.0 mux`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24%40v3.14.0+mux&patternType=keyword) | |**-repo:regexp-pattern**
_alias: -r_ | Exclude results from repositories whose path matches the regexp | `repo:alice/ -repo:old-repo` | | **rev:revision-pattern**
_alias: revision_ | Search a revision instead of the default branch. `rev:` can only be used in conjunction with `repo:` and may not be used more than once. See our [revision syntax](#repository-revisions) documentation to learn more | [`repo:sourcegraph/sourcegraph rev:v3.14.0 mux`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph+rev:v3.14.0+mux&patternType=keyword) | | **file:regexp-pattern**
_alias: f_ | Only include file results whose full path matches the regexp. The regexp is unanchored by default: to match against the entire path, use regexp anchors like `^README.md$`. | [`file:\.js$ httptest`](https://sourcegraph.com/search?q=file:%5C.js%24+httptest)
[`file:internal/ httptest`](https://sourcegraph.com/search?q=file:internal/+httptest) | | **-file:regexp-pattern**
_alias: -f_ | Exclude file results whose full path matches the regexp. The regexp is unanchored by default: to match against the entire path, use regexp anchors like `^README.md$`. | [`file:\.js$ -file:test http`](https://sourcegraph.com/search?q=file:%5C.js%24+-file:test+http) | | **content:"pattern"** | Set the search pattern with a dedicated parameter. Useful when searching literally for a string that may conflict with the [search pattern syntax](#search-pattern-syntax). In between the quotes, the `\` character will need to be escaped (`\\` to evaluate for `\`) | [`repo:sourcegraph content:"repo:sourcegraph"`](https://sourcegraph.com/search?q=repo:sourcegraph+content:"repo:sourcegraph"&patternType=keyword) | | **-content:"pattern"** | Exclude results from files whose content matches the pattern. | [`file:Dockerfile alpine -content:alpine:latest`](https://sourcegraph.com/search?q=file:Dockerfile+alpine+-content:alpine:latest&patternType=keyword) | | **select:_result-type_**
**select:repo**
**select:commit.diff.added**
**select:commit.diff.removed**
**select:file**
**select:content**
**select:symbol._symbol-type_**
**select:file.owners** _(Experimental)_ | Shows only query results for a given type. For example, `select:repo` displays only distinct repository paths from search results, and `select:commit.diff.added` shows only added code matching the search. See [language definition](/code-search/queries/language#select) for full list of possible values | [`fmt.Errorf select:repo`](https://sourcegraph.com/search?q=fmt.Errorf+select:repo&patternType=keyword) | | **language:language-name**
_alias: lang, l_ | Only include results from files in the specified programming language | [`language:typescript encoding`](https://sourcegraph.com/search?q=language:typescript+encoding) | | **-language:language-name**
_alias: -lang, -l_ | Exclude results from files in the specified programming language | [`-language:typescript encoding`](https://sourcegraph.com/search?q=-language:typescript+encoding) | | **type:symbol** | Perform a symbol search | [`type:symbol path`](https://sourcegraph.com/search?q=type:symbol+path) | | **case:yes** | Perform a case sensitive query. Without this, everything is matched case insensitively | [`OPEN_FILE case:yes`](https://sourcegraph.com/search?q=OPEN_FILE+case:yes) | | **fork:yes, fork:only** | Include results from repository forks or filter results to only repository forks. Results in repository forks are excluded by default | [`fork:yes repo:sourcegraph`](https://sourcegraph.com/search?q=fork:yes+repo:sourcegraph) | | **archived:yes, archived:only** | The yes option, includes archived repositories. The only option, filters results to only archived repositories. Results in archived repositories are excluded by default | [`repo:sourcegraph/ archived:only`](https://sourcegraph.com/search?q=repo:%5Egithub.com/sourcegraph/+archived:only) | | **repo:has.meta(...)** | **Experimental** Conditionally search inside repositories only if they are associated with a specified metadata:
1. key-value pair, or
2. key with any value, or
3. key with no value
See [built-in predicates](/code-search/queries/language#built-in-repo-predicate) for more | 1. `repo:has.meta(owning-team:security)`
2. `repo:has.meta(owning-team)`
3. `repo:has.meta(archived:)` | | **repo:has.path(...)** | Conditionally search inside repositories only if they contain a file path matching the regular expression. See [built-in predicates](/code-search/queries/language#built-in-repo-predicate) for more | [`repo:has.path(\.py) file:Dockerfile pip`](https://sourcegraph.com/search?q=context:global+repo:has.path%28%5C.py%29+file:Dockerfile+pip&patternType=keyword) | | **repo:has.topic(...)** | Search only in repos repositories if they have the given GitHub or GitLab topic. See [built-in predicates](/code-search/queries/language#built-in-repo-predicate) for more | [`repo:has.topic(code-search) rank`](https://sourcegraph.com/search?q=context:global+repo:sourcegraph/sourcegraph%24+rank&patternType=keyword&sm=1&groupBy=repo) | | **repo:has.commit.after(...)** | Filter out stale repositories that don't contain commits past the specified time frame. See [built-in predicates](/code-search/queries/language#built-in-repo-predicate) for more | [`repo:has.commit.after(yesterday)`](https://sourcegraph.com/search?q=context:global+repo:.*sourcegraph.*+repo:has.commit.after%28yesterday%29&patternType=keyword)
[`repo:has.commit.after(june 25 2017)`](https://sourcegraph.com/search?q=context:global+repo:.*sourcegraph.*+repo:has.commit.after%28june+25+2017%29&patternType=keyword) | | **rev:at.time(...)** | Search a repo or a branch at a specific point in time. | [`rev:at.time(1 year ago)`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:at.time%281+year+ago%29+&patternType=keyword&sm=0)
[`rev:at.time(2021-01-01)`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+rev:at.time%282021-01-01%2C+v5.0.0%29+&patternType=keyword) | | **file:has.content(...)** | Conditionally search files only if they contain contents that match the provided regex pattern. See [built-in predicates](/code-search/queries/language#built-in-repo-predicate) for more | [`file:has.content(Copyright) Sourcegraph`](https://sourcegraph.com/search?q=context:global+file:has.content%28Copyright%29+Sourcegraph&patternType=keyword) | | **file:has.owners(...)** | **Beta** Conditionally search files only if they are owned by the given owner. Empty means _any owner_. See [code ownership documentation](/own) for more | [`file:has.owner(alice@sourcegraph.com) Sourcegraph`](https://sourcegraph.com/search?q=context:global+file:has.owner%28alice@sourcegraph.com%29+Sourcegraph&patternType=keyword) | | **file:has.contributor(...)** | Conditionally search files only if a file contributor's name or email matches the provided regex pattern. See [built-in predicates](/code-search/queries/language#built-in-file-predicate) for more | [`file:has.contributor(alice@sourcegraph.com) Sourcegraph`](https://sourcegraph.com/search?q=context:global+file:has.owner%28alice@sourcegraph.com%29+Sourcegraph&patternType=keyword) | | **count:_N_,< /> count:all** | Retrieve N results. By default, Sourcegraph stops searching early and returns if it finds a full page of results. This is desirable for most interactive searches. To wait for all results, use **count:all** | [`count:1000 function`](https://sourcegraph.com/search?q=count:1000+repo:sourcegraph/sourcegraph$+function)
[`count:all err`](https://sourcegraph.com/search?q=repo:github.com/sourcegraph/sourcegraph+err+count:all&patternType=keyword) | | **timeout:_go-duration-value_** | Customizes the timeout for searches. The value of the parameter is a string that can be parsed by the [Go time package's `ParseDuration`](https://golang.org/pkg/time/#ParseDuration) (e.g. 10s, 100ms). By default, the timeout is set to 10 seconds, and the search will optimize for returning results as soon as possible. The timeout value cannot be set longer than 1 minute. When provided, the search is given the full timeout to complete | [`repo:^github.com/sourcegraph timeout:15s func count:10000`](https://sourcegraph.com/search?q=repo:%5Egithub.com/sourcegraph/+timeout:15s+func+count:10000) | | **patterntype:keyword, patterntype:regexp** | Configure your query to be interpreted as a keyword search, or a regular expression. Note: this filter is available as an accessibility option in addition to the visual toggles | [`test. patternType:keyword`](https://sourcegraph.com/search?q=test.+patternType:keyword)
[`(open/close)file patternType:regexp`](https://sourcegraph.com/search?q=%28open%7Cclose%29file&patternType=regexp) | | **visibility:any, visibility:public, visibility:private** | Filter results to only public or private repositories. The default is to include both private and public repositories | [`type:repo visibility:public`](https://sourcegraph.com/search?q=type:repo+visibility:public) | Multiple or combined **repo:** and **file:** filters are intersected. For example, `repo:foo repo:bar` limits your search to repositories whose path contains **both** `foo` and `bar` (such as `github.com/alice/foobar`). To include results from repositories whose path contains **either** `foo` or `bar`, use `repo:foo|bar`. The `rev:at.time(...)` filter is only supported for Sourcegraph versions 5.4 or more. ## Boolean operators Use boolean operators to create more expressive searches. | **Operator** | **Example** | | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `and`, `AND` | [`conf.Get( and log15.Error(`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+conf.Get%28+and+log15.Error%28&patternType=regexp), [`conf.Get( and log15.Error( and after`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+conf.Get%28+and+log15.Error%28+and+after&patternType=regexp) | Returns results for files containing matches on the left _and_ right side of the `and` (set intersection). | **Operator** | **Example** | | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `or`, `OR` | [`conf.Get( or log15.Error(`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+conf.Get%28+or+log15.Error%28&patternType=regexp), [conf.Get( or log15.Error( or after ](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+conf.Get%28+or+log15.Error%28+or+after&patternType=regexp) | Returns file content matching either on the left or right side, or both (set union). The number of results reports the number of matches of both strings. Note the regex or operator `|` may not work as expected with certain operators for example `file:(internal/repos)|(internal/gitserver)`, to receive the expected results use [subexpressions](/code-search/working/search_subexpressions), `(file:internal/repos or file:internal/gitserver)` | **Operator** | **Example** | | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `not`, `NOT` | [`lang:go not file:main.go panic`](https://sourcegraph.com/search?q=lang:go+not+file:main.go+panic&patternType=keyword), [`panic NOT ever`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+panic+not+ever&patternType=keyword) | `NOT` can be used in place of `-` to negate filters, such as `file`, `content`, `lang`, `repohasfile`, and `repo`. For search patterns, `NOT` excludes documents that contain the term after `NOT`. For readability, you can also include the `AND` operator before a `NOT` (i.e. `panic NOT ever` is equivalent to `panic AND NOT ever`). If you want to search for reserved keywords like `OR` in your code use `content` like this: `content:"query with OR"`. ### Operator precedence and groups Operators may be combined. `and` expressions have higher precedence (bind tighter) than `or` expressions so that `a and b or c and d` means `(a and b) or (c and d)`. Expressions may be grouped with parentheses to change the default precedence and meaning. For example: `a and (b or c) and d`. ### Filter scope When parentheses are absent, we use the convention that operators divide sequences of terms that should be grouped together. That is: `file:main.c char c or (int i and int j)` generally means `(file:main.c char c) or (int i and int j)` To apply the scope of the `file` filter to the entire subexpression, fully group the subexpression: `file:main.c (char c or (int i and int j))`. Browse the [search subexpressions examples](/code-search/working/search_subexpressions) to learn more about use cases. ## Filters (diff and commit searches only) The following filters are only used for **commit diff** and **commit message** searches, which show changes over time: | **Filter** | **Description** | **Examples** | | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | **repo:regexp-pattern@rev** | Specifies which Git revisions to search for commits. See our [repository revisions](#repository-revisions) documentation to learn more about the revision syntax. | [`repo:vscode@*refs/heads/:^refs/heads/master type:diff task`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/Microsoft/vscode%24%40*refs/heads/:%5Erefs/heads/master+type:diff+after:%221+month+ago%22+task#1) (unmerged commit diffs containing `task`) | | **type:diff**
**type:commit** | Specifies the type of search. By default, searches are executed on all code at a given point in time (a branch or a commit). Specify the `type:` if you want to search over changes to code or commit messages instead (diffs or commits). | [`type:diff func`](https://sourcegraph.com/search?q=type:diff+func+repo:sourcegraph/sourcegraph$)
[`type:commit test`](https://sourcegraph.com/search?q=type:commit+test+repo:sourcegraph/sourcegraph$) | | **author:name** | Only include results from diffs or commits authored by the user. Regexps are supported. Note that they match the whole author string of the form `Full Name `, so to include only authors from a specific domain, use `author:example.com>$`. You can also use `author:@SourcegraphUserName` to search on a Sourcegraph user's list of verified emails.

You can also search by `committer:git-email`. _Note: there is a committer only when they are a different user than the author._ | [`type:diff author:nick`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick) | | **-author:name** | Exclude results from diffs or commits authored by the user. Regexps are supported. Note that they match the whole author string of the form `Full Name `, so to exclude authors from a specific domain, use `author:example.com>$`. You can also use `author:@SourcegraphUserName` to search on a Sourcegraph user's list of verified emails.

You can also search by `committer:git-email`. _Note: there is a committer only when they are a different user than the author._ | [`type:diff author:nick`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick) | | **before:"string specifying time frame"** | Only include results from diffs or commits which have a commit date before the specified time frame | [`before:"last thursday"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick+before:%22last+thursday%22)
[`before:"november 1 2019"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick+before:%22november+1+2019%22) | | **after:"string specifying time frame"** | Only include results from diffs or commits which have a commit date after the specified time frame | [`after:"6 weeks ago"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick+after:%226+weeks+ago%22)
[`after:"november 1 2019"`](https://sourcegraph.com/search?q=repo:sourcegraph/sourcegraph$+type:diff+author:nick+after:%22november+1+2019%22) | | **message:"any string"** | Only include results from diffs or commits which have commit messages containing the string | [`type:commit message:"testing"`](https://sourcegraph.com/search?q=type:commit+repo:sourcegraph/sourcegraph$+message:%22testing%22)
[`type:diff message:"testing"`](https://sourcegraph.com/search?q=type:diff+repo:sourcegraph/sourcegraph$+message:%22testing%22) | | **-message:"any string"** | Exclude the results from diffs or commits which have commit messages containing the string | [`type:commit message:"testing"`](https://sourcegraph.com/search?q=type:commit+repo:sourcegraph/sourcegraph$+message:%22testing%22)
[`type:diff message:"testing"`](https://sourcegraph.com/search?q=type:diff+repo:sourcegraph/sourcegraph$+message:%22testing%22) | ## Repository Search ### Repository Revisions To search revisions other than the default branch, specify the revisions by either appending them to the `repo:` filter or by listing them separately with the `rev:` filter. This means: `repo:github.com/myteam/abc@` is equivalent to `repo:github.com/myteam/abc rev:`. The `` part refers to repository revisions (branches, commit hashes, and tags) and may take on the following forms: (All examples apply to `@` as well as `rev:`) - `@branch` - a branch name - `@1735d48` - a commit hash - `@3.15` - a tag You can separate revisions by a colon to search multiple revisions at the same time, `@branch:1735d48:3.15`. Per default, we match revisions to tags, branches, and commits. You can limit the search to branches or tags by adding the prefix `refs/tags` or `refs/heads`. For example `@refs/tags/3.18` will search the commit tagged with `3.18`, but not a branch called `3.18` and vice versa for `@refs/heads/3.18`. **Glob patterns** allow you to search over a range of branches or tags. Prepend `*` to mark a revision as glob pattern and add the glob-pattern after it like this `repo:@*`. For example: - [`@*refs/heads/*`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/docker/machine%24%40*refs/heads/*+middleware&patternType=keyword) - search across all branches - [`@*refs/tags/*`](https://sourcegraph.com/search?q=repo:github.com/docker/machine%24%40*refs/tags/*+server&patternType=keyword) - search across all tags We automatically add a trailing `/*` if it is missing from the glob pattern. You can negate a glob pattern by prepending `*!`, for example: - [`@*refs/heads/*:*!refs/heads/release* type:commit `](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/kubernetes/kubernetes%24%40*refs/heads/*:*%21refs/heads/release*+type:commit+&patternType=keyword) - search commits on all branches except on those that start with "release" - [`@*refs/tags/v3.*:*!refs/tags/v3.*-* context`](https://sourcegraph.com/search?q=repo:%5Egithub.com/sourcegraph/sourcegraph%24%40*refs/tags/v3.*:*%21refs/tags/v3.*-*+context&patternType=keyword) - search all versions starting with `3.` except release candidates, alpha and beta versions. ### Repository names A query with only `repo:` filters returns a list of repositories with matching names. - Example: [`repo:docker repo:registry`](https://sourcegraph.com/search?q=repo:docker+repo:registry) matches repositories with names that contain both `docker` and `registry` substrings - Example: [`repo:docker OR repo:registry`](https://sourcegraph.com/search?q=repo:docker+OR+repo:registry&patternType=keyword) matches repositories with names that contain either `docker` or `registry` substrings ### Commit and Diff searches Commit and diff searches act on sets of commits. A set is defined by a revision (branch, commit hash, or tag), and it contains all commits reachable from that revision. A commit is reachable from another commit if it can be reached by following the pointers to parent commits. For commit and diff searches it is possible to exclude a set of commits by prepending a caret `^`. The caret acts as a set difference. For example, `repo:github.com/myteam/abc@main:^3.15 type:commit` will show all commits in `main` minus the commits reachable from the commit tagged with `3.15`. ## Filename Search A query with `type:path` restricts terms to matching filenames only (not file contents). - Example: [`type:path repo:/docker/ registry`](https://sourcegraph.com/search?q=type:path+repo:/docker/+registry) ## Content Search A query with `type:file` restricts terms to matching file contents only (not filenames). - Example: [`type:file repo:^github\.com/sourcegraph/about$ website`](https://sourcegraph.com/search?q=type:file+repo:%5Egithub%5C.com/sourcegraph/about%24+website&patternType=keyword)
# Search Examples

Learn and walkthrough some of the handy Code Search examples.

## Sourcegraph Search Examples on GitHub Pages Check out the [Sourcegraph Search Examples](https://sourcegraph.github.io/sourcegraph-search-examples/) site for filterable search examples with links to results on sourcegraph.com. Below are some additional examples that search repositories on [Sourcegraph.com](https://sourcegraph.com/search), our open source code search solution for GitHub and GitLab. You can copy and adapt the following search queries for use on your company’s private instance. See [search query syntax](/code-search/queries) reference. [Search through all the repositories in an organization](https://sourcegraph.com/search?q=context:global+r:hashicorp/+terraform&patternType=keyword&groupBy=repo) ```sgquery context:global r:hashicorp/ terraform ``` [Search a subset of repositories in an organization by language](https://sourcegraph.com/search?q=context:global+r:hashicorp/vault*+lang:yaml+terraform&patternType=keyword&groupBy=repo) ```sgquery context:global r:hashicorp/vault* lang:yaml terraform ``` [Search for one term or another in a specific repository](https://sourcegraph.com/search?q=context:global+r:hashicorp/vault%24+print%28+OR+log%28&patternType=keyword&groupBy=repo) ```sgquery context:global r:hashicorp/vault$ print( OR log( ``` [Find private keys and GitHub access tokens checked in to code](https://sourcegraph.com/search?q=context:global+%28-----BEGIN+%5BA-Z+%5D*PRIVATE+KEY------%29%7C%28%28%22gh%7C%27gh%29%5Bpousr%5D_%5BA-Za-z0-9_%5D%7B16%2C%7D%29&patternType=regexp&case=yes) ```sgquery (-----BEGIN [A-Z ]*PRIVATE KEY------)|(("gh|'gh)[pousr]_[A-Za-z0-9_]{16,}) patternType:regexp case:yes ``` [Recent security-related changes on all branches](https://sourcegraph.com/search?q=type:diff+repo:github%5C.com/kubernetes/kubernetes%24+repo:%40*refs/heads/+after:"5+days+ago"+%5Cb%28auth%5B%5Eo%5D%5B%5Er%5D%7Csecurity%5Cb%7Ccve%7Cpassword%7Csecure%7Cunsafe%7Cperms%7Cpermissions%29&patternType=regexp) ```sgquery type:diff repo:@*refs/heads/ after:"5 days ago" \b(auth[^o][^r]|security\b|cve|password|secure|unsafe|perms|permissions) ``` [Admitted hacks and TODOs in app code](https://sourcegraph.com/search?q=-file:%5C.%28json%7Cmd%7Ctxt%29%24+hack%7Ctodo%7Ckludge%7Cfixme&patternType=regexp) ```sgquery -file:\.(json|md|txt)$ hack|todo|kludge|fixme ``` [Removal of TODOs in the repository commit log](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+type:diff+TODO+select:commit.diff.removed+&patternType=keyword) ```sgquery repo:^github\.com/sourcegraph/sourcegraph$ type:diff select:commit.diff.removed TODO ``` [New usages of a function](https://sourcegraph.com/search?q=repo:github%5C.com/sourcegraph/+type:diff+after:%221+week+ago%22+%5C.subscribe%5C%28+lang:typescript&patternType=regexp) ```sgquery type:diff after:"1 week ago" \.subscribe\( lang:typescript ``` [Find multiple terms in the same file, like testing of HTTP components](https://sourcegraph.com/search?q=repo:github%5C.com/sourcegraph/sourcegraph%24+%28test+AND+http+AND+NewRequest%29+lang:go&patternType=regexp) ```sgquery repo:github\.com/sourcegraph/sourcegraph$ (test AND http AND NewRequest) lang:go ``` [Recent quality related changes on all branches (customize for your linters)](https://sourcegraph.com/search?q=repo:github%5C.com/sourcegraph/+repo:%40*refs/heads/:%5Emaster+type:diff+after:"1+week+ago"+%28eslint-disable%29&patternType=regexp) ```sgquery repo:@*refs/heads/:^master type:diff after:"1 week ago" (eslint-disable) ``` [Recent dependency changes](https://sourcegraph.com/search?q=repo:github%5C.com/sourcegraph/+file:package.json+type:diff+after:%221+week+ago%22) ```sgquery file:package.json type:diff after:"1 week ago" ``` [Files that are Apache licensed](https://sourcegraph.com/search?q=context:global+%22licensed+to+the+apache+software+foundation%22+select:file&patternType=keyword) ```sgquery licensed to the apache software foundation select:file ``` [Only _repositories_ with recent dependency changes](https://sourcegraph.com/search?q=repo:github%5C.com/sourcegraph/+file:package.json+type:diff+after:%221+week+ago%22+select:repo&patternType=regexp) ```sgquery file:package.json type:diff after:"1 week ago" select:repo ``` [Search changes in a files that contain a keyword](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/sourcegraph%24+type:diff+file:contains.content%28%22golang%5C.org/x/sync/errgroup%22%29+.Go&patternType=keyword) ```sgquery repo:^github\.com/sourcegraph/sourcegraph$ type:diff file:contains.content("golang\.org/x/sync/errgroup") .Go ``` ## When to use regex search mode Sourcegraph's default literal search mode is line-based and will not match across lines, so regex can be useful when you wish to do so: [Matching multiple text strings in a file](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/Parsely/pykafka%24+Not+leader+for+partition&patternType=regexp) ```sgquery repo:^github\.com/Parsely/pykafka$ Not leader for partition ``` Regex searches are also useful when searching boundaries that are not delimited by code structures: [Finding css classes with word boundary regex](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/sourcegraph/+%5Cbbtn-secondary%5Cb&patternType=regexp) ```sgquery repo:^github\.com/sourcegraph/ \bbtn-secondary\b ``` More examples of regex searches are available [here](https://sourcegraph.com/docs/code-search/queries#regular-expression-search)
# Switching from Oracle OpenGrok to Sourcegraph > NOTE: This guide helps Sourcegraph users switch from using Oracle OpenGrok's search syntax to Sourcegraph's. See our [Oracle OpenGrok admin migration guide](/admin/migration/opengrok) for instructions on switching from an OpenGrok deployment to Sourcegraph. ## Related pages - [All Sourcegraph search documentation](/code-search/) - [Sourcegraph query syntax](/code-search/queries) - [Search product feature comparisons](https://about.sourcegraph.com/workflow) - [OpenGrok to Sourcegraph admin migration guide](/admin/migration/opengrok) ## Switching to Sourcegraph's query syntax ### Wildcards vs. regular expressions Oracle OpenGrok provides wildcard support for searches. For example, to find all strings beginning with `foo`, you can use the wildcard search `foo*`. Similarly, OpenGrok provides the `?` operator for single character wildcards. Sourcegraph, [provides full regular expression search](/code-search/queries#regexp-search), with support for the [RE2 syntax](https://golang.org/s/re2syntax). The same search above would take the form `foo.*` (or in this case, just `foo`, since Sourcegraph automatically supports partial matches). Much more powerful regexp expressions are available. (Note that Sourcegraph also provides a [literal search mode](/code-search/queries#Literal-search-default) by default, in which there's no need to escape special characters. This simplifies searches such as `foo(`, which would result in an error in regexp mode.) ### Selecting repositories and branches Oracle OpenGrok provides a multi-select dropdown box to allow users to select which repositories to include in a search. This scope is stored across sessions, until the user changes it. Sourcegraph provides a search filter (`repo:`) that supports regexp and partial matches for selecting repositories. As examples: - To search for the string "pattern" in all repositories in the github.com/org org, search for `pattern repo:github.com/org`. | - To search in a distinct list of repositories, you can use a ` | ` character as a regexp OR operator: `pattern repo:github.com/org/repository1 | github.com/org/repository2`. | | - Note this query could be simplified further using more advanced regexp matching if the two repos share part of their names, such as: `pattern repo:github.com/org/repository(1 | 2)`. | | Sourcegraph also allows site admins to create pre-defined repository groupings, using [version contexts](/code-search/features#version-contexts-experimental). ### Searching in non-master (unindexed) branches, tags, and commits Oracle OpenGrok can only search in the version of code that is stored on disk. To search across multiple revisions or branches, an OpenGrok administrator must explicitly add each of those copies of the code to OpenGrok. Sourcegraph allows users to search on any Git revision, even if it is not indexed. Users can append `@` to the end of any `repo:` filter to specify which version to search. For example, to search on `feature-branch`, use `pattern repo:github.com/org/repo@feature-branch`, and to search on a commit `abc123`, use `pattern repo:github.com/org/repo@abc123`. Sourcegraph also provides the ability to search on multiple Git revisions in a single repository at once, using `:` characters to separate revision names in the `repo:` field. For example, search both `feature-branch` and `abc123` in a single query using `pattern repo:github.com/org/repo@feature-branch:abc123`. ### Special characters Oracle OpenGrok doesn't index most single-character strings (such as for special characters like `{`, `}`, `[`, `]`, `+`, `-`, and more), and non-alpha-numeric characters generally. Sourcegraph indexes all characters, and can search for strings of any length. Using the default [literal search mode](/code-search/queries#Literal-search-default), any search (including those with special characters like `foo.bar`, `try {`, `i++`, `i-=1`, `foo->bar`, and more), will all be searchable without special handling. Using [regexp mode](/code-search/queries#regexp-search) would require escaping special charactes. The only exceptions are colon characters, which are by default used for specifying a [search keyword](#search-keywords) on Sourcegraph. Any search containing colons can be done using the `content:` keyword (for example, `content:"foo::bar"`) to explicitly mark it as the search string. ### Boolean operators Oracle OpenGrok provides three boolean operators — `AND`, `OR`, and `NOT` — for scoping searches to files that contain strings that match multiple patterns. Sourcegraph also provides [`AND`, `OR`, and `NOT` operators](/code-search/queries#boolean-operators). > NOTE: Operators are available as of Sourcegraph 3.17 ### Search filters Both Sourcegraph and OpenGrok allow users to add filters for scoping searches. Below is a mapping from OpenGrok syntax to Sourcegraph. | | OpenGrok | Sourcegraph | | -------------------------------------------------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------------------- | | Search text | `full:pattern` | `pattern` | | Search symbol definitions | `def:pattern` | `pattern type:symbol` | | Search symbol references | `def:pattern` | Available through hover tooltips and `Find references` panels on code pages | | Search for repository names | Not supported | `pattern type:repo` | | Search for file names | `file:pattern` | `file:pattern` | | Search commit messages | `hist:pattern` | `pattern type:commit` | | Search code changes (diff search) | Not supported | `pattern type:diff` | | Scope searches to a language | `pattern type:c` | `pattern lang:c` | | Case sensitivity | Not supported | `case:yes` or `case:no` | | Scope searches to forked repositories | Supported through the repository selector | `fork:yes`, `fork:no`, or `fork:only` | | Scope searches to archived repositories | Supported through the repository selector | `archived:yes`, `archived:no`, or `archived:only` | | Scope searches to repositories that contain a file | Not supported | `pattern repo:contains.file(README)` | | Scope searches to repositories that contain file content | Not supported | `pattern repo:contains.content(TODO)` | | Scope searches to recently updated repositories | Not supported | `pattern repo:contains.commit.after(3 months)` or `pattern repo:contains.commit.after(june 25 2017)` | Sourcegraph also provides keywords to [scope commit message and diff searches](/code-search/queries#filters-diff-and-commit-searches-only) to specific authors or timeframes in which a change was made. To see an exhaustive list of Sourcegraph's search filters, see the [search query syntax](/code-search/queries#filters-all-searches) page. # How-to guides - [Switch from Oracle OpenGrok to Sourcegraph](/code-search/how-to/opengrok) - [How to create a search context with the GraphQL API](/code-search/how-to/create_search_context_graphql) # How to create a search context with the GraphQL API This document will take you through how to create a search context for your user with Sourcegraph's GraphQL API. ## Prerequisites * This document assumes that you have a private Sourcegraph instance * Assumes you are creating a Private search context with a user namespace which is only available to the user * For more information on available permissions and ways to alter the following examples, please see [Managing search contexts with api - permissions and visibility overview](/api/graphql/managing-search-contexts-with-api#permissions-and-visibility-overview) ## Steps to create Step 1: Add to global configuration (must be site-admin): ```json { "experimentalFeatures": { "showSearchContext": true } } ``` Step 2: Make sure you have added code hosts: [Add repositories (from code hosts) to Sourcegraph](/admin/repo/add) Step 3: Follow the steps to [Generate an access token for GraphQL](/api/graphql#quickstart) if you already haven't Step 4: Navigate to the API console on your instance, replacing sourcegraph.example with the correct string for your instance URL. * Example: `https://sourcegraph.example.com/api/console` Step 5: Query your user namespace id and save the value * The name: will be your Sourcegraph instance login name Example: ```json query { namespaceByName(name: "my_login_name") { id } } ``` Step 6: Query your desired repo id and save the value. * It should be whatever the URL is for that repo. Example: ```json query { repository(name: "github.com/org_name/repo_name") { id } } ``` Step 7: Take the values from steps 5 and 6 and put them into the example variables from our docs here: * [Managing search contexts with API - Create a context](/api/graphql/managing-search-contexts-with-api#create-a-context) Run this with no changes: ```json mutation CreateSearchContext( $searchContext: SearchContextInput! $repositories: [SearchContextRepositoryRevisionsInput!]! ) { createSearchContext(searchContext: $searchContext, repositories: $repositories) { id spec } } ``` Then in the Query Variables section on the bottom of the GraphQL API page, use this variables example, changing at least the name and description: ```json { "searchContext": { "name": "MySearchContext", "description": "A description of my search context", "namespace": "user-id-from-step-5", "public": true }, "repositories": [ { "repositoryID": "repo-id-from-step-6", "revisions": ["main", "branch-1"] } ] } ``` Step 8: Run the query, that should create a search context and the output will look something like: ```json { "data": { "createSearchContext": { "id": "V2VhcmNoQ29udGV4dDoiQGdpc2VsbGUvTXlTZWFyY2hDb250ZXh0MiI=", "spec": "@my_login_name/MySearchContext" } } } ``` Step 9: Go to the main search page and you should see the new Search context as part of the search bar! ## Further resources * [Using and creating search contexts](/code-search/working/search_contexts) * [Sourcegraph - Administration Config](/admin/config) # Indexers

This page describes the process of writing an indexer and details all the recommended indexers that Sourcegraph currently supports.

## Writing an Indexer The following documentation describes the [SCIP Code Intelligence Protocol](https://github.com/sourcegraph/scip) and explains steps to write an indexer to emit SCIP. 1. Familiarize yourself with the [SCIP protobuf schema](https://github.com/sourcegraph/scip/blob/main/scip.proto) 2. Import or generate SCIP bindings 3. Generate minimal index with occurrence information 4. Test your indexer using [scip CLI](https://github.com/sourcegraph/scip/blob/main/docs/CLI.md)'s `snapshot` subcommand 5. Progressively add support for more features with tests If you run into problems or have questions for any of these steps, please open an issue on the [SCIP issue tracker](https://github.com/sourcegraph/scip/issues). Let's understand each of these steps in detail: ## Understanding the SCIP protobuf schema The [SCIP protobuf schema](https://github.com/sourcegraph/scip/blob/main/scip.proto) describes the structure of a SCIP index in a machine-readable format. The main structure is an [`Index`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/scip%24+%5Emessage+Index+%7B%5Cn%28.%2B%5Cn%29%2B%7D&patternType=regexp) which consists of a list of documents along with some metadata. Optionally, an index can also provide hover documentation for external symbols that will not be indexed. A [`Document`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/scip%24+message+Document+%7B...%7D&patternType=structural) has a unique path relative to the project root. It also has a list of occurrences, which attach information to source ranges, as well as a list of symbols that are defined in the document. The information covered by an [`Occurrence`](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/sourcegraph/scip%24+message+Occurrence+%7B...%7D&patternType=structural) can be syntactic or semantic: - Syntactic information such as the `syntax_kind` field is used for highlighting - Semantic information such as the `symbol` and `symbol_role` fields are used to power code navigation features like Go to definition and Find references Occurrences also allow attaching diagnostic information, which can be used by static analysis tools. For more details, see the doc comments in the [SCIP protobuf schema](https://github.com/sourcegraph/scip/blob/main/scip.proto). You may also find it helpful to see how existing indexers emit information. For example, you can take a look at the [scip-typescript](https://github.com/sourcegraph/scip-typescript) or [scip-java](https://github.com/sourcegraph/scip-java) code to see how they emit SCIP indexes. ## Importing or generating SCIP bindings The SCIP repository contains bindings for several languages. Depending on your indexer's implementation language, you can import the bindings directly using your language's package manager, or by using git submodules. One benefit of this approach is that you do not need to have a protobuf toolchain to generate code from the schema. This also makes it easier to bump the version of SCIP to pick up newer changes to the schema. Alternately, you can vendor the SCIP protobuf schema into your repository and set up Protobuf generation yourself. This has the benefit of being able to control the process from end-to-end, at the cost of making updates a bit more cumbersome. Newer Sourcegraph versions will maintain backwards compatibility with older SCIP versions, so there is no risk of not being able to upload SCIP indexes if a vendored schema has not been updated in a while. ## Generating minimal index with occurrence information As a first pass, it's recommended generating occurrences for a subset of declarations and checking that the generation works from end-to-end. In the context of an indexer, this typically involves using a compiler frontend or a language server as a library. First, run the compiler pipeline until semantic analysis is completed. Next, perform a top-down traversal of ASTs for all files, recording information about different kinds of occurrences. At the end, write a conversion pass from the intermediate data to SCIP using the SCIP bindings. As a convention, indexers should use `index.scip` as the default filename for the output. The [Sourcegraph CLI](https://github.com/sourcegraph/src-cli) recognizes this filename and uses it as the default upload path. You can inspect the Protobuf output using `protoc`: ```sh # assuming scip.proto and index.scip are in the current directory protoc --decode=scip.Index scip.proto < index.scip ``` For robust testing, it's recommended making sure that the result of indexing is deterministic. One potential source of issues here is non-determinstic iteration over the key-value pairs of a hash table. If re-running your indexer changes the order in which occurrences are emitted, snapshot testing may report different results. ## Snapshot testing with scip CLI One of the key design criteria for SCIP was that it should be easy to understand an index file and test an indexer for correctness. The [scip CLI](https://github.com/sourcegraph/scip/blob/main/docs/CLI.md) has a `snapshot` subcommand which can be used for golden testing. It `snapshot` command inspects an index file and regenerates the source code, attaching comments describing occurrence information. Here is slightly cleaned up snippet from running `scip snapshot` on the index generated by running `scip-typescript` over itself: ```ts function scriptElementKind( // ^^^^^^^^^^^^^^^^^ definition scip-typescript npm @sourcegraph/scip-typescript 0.2.0 src/FileIndexer.ts/scriptElementKind(). node: ts.Node, // ^^^^ definition scip-typescript npm @sourcegraph/scip-typescript 0.2.0 src/FileIndexer.ts/scriptElementKind().(node) // ^^ reference local 1 // ^^^^ reference scip-typescript npm typescript 4.6.2 lib/typescript.d.ts/ts/Node# sym: ts.Symbol // ^^^ definition scip-typescript npm @sourcegraph/scip-typescript 0.2.0 src/FileIndexer.ts/scriptElementKind().(sym) // documentation ```ts // ^^ reference local 1 // ^^^^^^ reference scip-typescript npm typescript 4.6.2 lib/typescript.d.ts/ts/Symbol# ): ts.ScriptElementKind { // ^^ reference local 1 // ^^^^^^^^^^^^^^^^^ reference scip-typescript npm typescript 4.6.2 lib/typescript.d.ts/ts/ScriptElementKind# ``` The carets and contextual information make it easy to visually check that: - Occurrences are being emitted for the right source ranges - Occurrences have the expected symbol strings. The exact syntax for the symbol strings is described in the doc comment for [`Symbol`](https://sourcegraph.com/github.com/sourcegraph/scip@12459c75fc15117e68b4c15a58e8581b738b855f/-/blob/scip.proto?L87-115) in the SCIP Protobuf schema - Symbols correspond to the right package. For example, the `ScriptElementKind` is defined in the `typescript` package (the compiler) whereas `scriptElementKind` is defined in `@sourcegraph/scip-typescript` ## Progressively adding support for language features It's recommended adding support for different features in the following order: 1. Emit occurrences and symbols for a single file. - Iterate over different kinds of entities (functions, classes, properties etc.) 1. Emit hover documentation for entities. If the markup is in a format other than CommonMark, we recommend addressing that difference after addressing other features. 1. Add support for implementation relationships, enabling Find implementations. 1. (Optional) If the hover documentation uses markup in a format other than CommonMark, implement a conversion from the custom markup language to CommonMark. If implementing an indexer in C++, you need to careful about very large indexes, as the default Protoc-generated code has a 2GB implementation limit on message sizes. This limit can be worked around by writing documents to disk one-by-one. For example, this PR [adds support to scip-ruby for writing documents one-by-one](https://github.com/sourcegraph/scip-ruby/pull/190). ## Sourcegraph recommended indexers Language support is an ever-evolving feature of Sourcegraph. Some languages may be better supported than others due to demand or developer bandwidth/expertise. The following clarifies the status of the indexers which the Sourcegraph team can both recommend to customers and provide support for. ### Quick reference This table is maintained as an authoritative resource for users, Sales, and Customer Engineers. Any major changes to the development of our indexers will be reflected here. | **Language** | **Indexer** | **Status** | **Hover docs** | **Go to definition** | **Find references** | **Cross-file** | **Cross-repository** | **Find implementations** | | --------------------- | ----------------- | ------ | ---------- | ---------------- | --------------- | ---------- | ---------------- | -------------------- | | Go | [scip-go] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | TypeScript/
JavaScript | [scip-typescript] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | C/C++ | [scip-clang] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Java | [scip-java] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Scala | [scip-java] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Kotlin | [scip-java] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | | Rust | [rust-analyzer] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | | Python | [scip-python] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | Ruby | [scip-ruby] | 🟢 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | C# | [scip-dotnet] Build tools (`.sln`, `.csproj`) | 🟢 | ✓ | ✓ | ✓ | ✓ | ✗ | ✓ | [scip-go]: https://github.com/sourcegraph/scip-go [scip-typescript]: https://github.com/sourcegraph/scip-typescript [scip-clang]: https://github.com/sourcegraph/scip-clang [scip-java]: https://github.com/sourcegraph/scip-java [scip-ruby]: https://github.com/sourcegraph/scip-ruby [scip-python]: https://github.com/sourcegraph/scip-python [scip-dotnet]: https://github.com/sourcegraph/scip-dotnet [rust-analyzer]: https://rust-analyzer.github.io/ Requires enabling and setting up Sourcegraph's auto-indexing feature. Read more about [Auto-indexing](/code-search/code-navigation/auto_indexing). ### Status definitions An indexer status is: - 🟢 **Generally Available**: Available as a normal product feature, no major features are absent. - 🟡 **Partially Available**: Available, but may be limited in some significant ways. No major features are absent but edge cases remain. - 🟠 **Beta**: Available in pre-release form on a limited basis. Could be useful to early adopters despite lack of features. - 🟣 **Experimental**: Available in pre-release form, with significant caveats. Early adopters can try it with expectations of failure. ## Milestone definitions A common set of steps required to build feature-complete indexers is broadly outlined below. The implementation order and _doneness criteria_ of these steps may differ between language and development ecosystems. Major divergences will be detailed in the notes below. ### Cross repository: Emits monikers for cross-repository support The next milestone provides support for cross-repository definitions and references. The indexer can emit a valid index including import monikers for each symbol defined non-locally, and export monikers for each symbol importable by another repository. This index should be consumed without error by the latest Sourcegraph instance and Go to Definition and Find References should work on cross-repository symbols given that **both repositories are indexed at the exact commit imported**. At this point, the indexer may be generally considered **ready**. Some languages and ecosystems may require some of the additional following milestones to be considered ready due to a bad out-of-the-box developer experience or absence of a critical language features. For example, `scip-java` is nearly useless without built-in support for build systems such as gradle. ### Common build tool integration The next milestone integrates the indexer with a common build tool or framework for the language and surrounding ecosystem. The priority and applicability of this milestone will vary wildly by languages. For example, `scip-go` uses the standard library and the language has a built-in dependency manager; all of our customers use Gradle, making `scip-java` effectively unusable without native Gradle support. The indexer integrates natively with common mainstream build tools. We should aim to cover at least the majority of build tools used by existing enterprise customers. The implementation of this milestone will also vary wildly by language. It could be that the indexer runs as a step in a larger tool, or the indexer contains build-specific logic in order to determine the set of source code that needs to be analyzed. ### The long tail: Find implementations The next milestone represents the never-finished long tail of feature additions. The **remaining 20%** of the language should be supported via incremental updates and releases over time. Support of Find implementations should be prioritized by popularity and demand. We may lack the language expertise or bandwidth to implement certain features on indexers. We will consider investing resources to add additional features when the demand for support is sufficiently high and the implementation is sufficiently difficult. ## More resources
# Code Navigation troubleshooting guide

This guide gives specific instructions for troubleshooting code navigation in your Sourcegraph instance.

## When are issues related to code intelligence? Issues are related to Sourcegraph code navigation when the [indexer](/code-search/code-navigation/writing_an_indexer) is one that we build and maintain. A customer issue should **definitely** be routed to code navigation if any of the following are true. - Precise code navigation queries are slow - Precise code navigation queries yield unexpected results A customer issue should **possibly** be routed to code navigation if any of the following are true. - Search-based code navigation queries are slow - Search-based code navigation queries yield unexpected results A customer issue should **not** be routed to code navigation if any of the following are true. - The indexer is listed in [LSIF.dev](https://lsif.dev/) and _it is not_ one that we maintain. Instead, flag the indexers status and maintainer of the relevant indexer with the customer, and suggest they reach out directly ## Gathering evidence Before bringing a code navigation issue to the engineering team, the site-admin or customer engineer should gather the following details. Not all of these details will be necessary for all classes of errors. ### Sourcegraph instance details The following details should always be supplied. - The Sourcegraph instance version - The Sourcegraph instance deployment type (e.g. server, pure-docker, docker-compose, k8s) - The memory, cpu, and disk resources allocated to the following containers: - frontend - precise-code-intel-worker - codeintel-db - blobstore (or `minio` in versions of Sourcegraph before v3.4.2) If the customer is running a custom patch or an insiders version, we need the docker image tags and SHAs of the following containers: - frontend - precise-code-intel-worker ### Sourcegraph CLI details The following details should be supplied if there is an issue with _uploading_ LSIF indexes to their instance. - The Sourcegraph CLI version ```bash $ src version Current version: 3.26.0 Recommended Version: 3.26.1 ``` ### Settings The following user settings should be supplied if there is an issue with _displaying_ code navigation results. Only these settings should be necessary, but additional settings can be supplied after private settings such as passwords or secret keys have been removed. - `codeIntel.lsif` - `codeIntel.traceExtension` - `codeIntel.disableRangeQueries` - `basicCodeIntel.includeForks` - `basicCodeIntel.includeArchives` - `basicCodeIntel.indexOnly` - `basicCodeIntel.unindexedSearchTimeout` You can get your effective user settings (site-config + user settings override) with the following Sourcegraph CLI command. ```bash $ src api -query 'query ViewerSettings { viewerSettings { final } }' ``` If you have [jq](https://stedolan.github.io/jq/) installed, you can unwrap the data more easily. ```bash src api -query 'query ViewerSettings { viewerSettings { final } }' | jq -r '.data.viewerSettings.final' | jq ``` ### Traces [Jaeger](/admin/observability/tracing) traces should be supplied if there is a noticeable performance issue in receiving code navigation results in the SPA. Depending on the type of user operation that is slow, we will need traces for different request types. | **Send traces for requests** | **When latency is high** | | --------------------------------- | ------------------------------------------------------------------- | | `?DefinitionAndHover`, `?Ranges` | between hovering over an identifier and receiving hover text | | `?References` | between querying references and receiving the first result | | `?Ranges` | between hovering over an identifier and getting document highlights | To gather a trace from the SPA, open your browser's developer tools, open the network tab, then add `?trace=1` to the URL and refresh the page. Note that if the URL contains a query fragment (such as `#L24:27`), the query string must go **before** the leading hash. Hovering over identifiers in the source file should fire off requests to the API. Find a request matching the target type (given in the table above). If there are multiple matching requests, prefer the ones with higher latencies. The `x-trace` header should have a URL value that takes you a detailed view of that specific request. This trace is exportable from the Jaeger UI. ![Network waterfall](https://storage.googleapis.com/sourcegraph-assets/Docs/network-waterfall.png) ![Request headers](https://storage.googleapis.com/sourcegraph-assets/Docs/network-description.png)
# Syntactic Code Navigation Supported on [Enterprise](/pricing/enterprise) plans. This feature is currently in Beta and enabled by default for Cloud customers Syntactic code navigation is a zero configuration feature that improves code navigation for certain languages in the absence of [Precise code navigation](./precise_code_navigation) set up. It works by periodically indexing repositories for which it is [enabled](#enabling-syntactic-code-navigation), using high level syntax analysis heuristics. This mechanism is more robust than search-based navigation, but less powerful than Precise code navigation. When syntactic indexing data is available for a given file and repository, it will be selected over search-based navigation automatically, but only if Precise data is not available. To summarize, the order of priorities is as follows: - Precise data - Syntactic, if no precise data available - Search-based, if no syntactic available ## Enabling Syntactic code navigation First, enable the experimental feature flag in Site config: ```json "experimentalFeatures.codeintelSyntacticIndexing.enabled": true ``` Next, you need to ensure that syntactic indexing is enabled in a code graph configuration policy. Note that for cloud customers this feature is enabled by default for all repositories, as part of automatically managed policy called `[Sourcegraph Managed] Head of default branch policy`. For all other deployments, you will need to create a HEAD policy with "Syntactic indexing" enabled: ![Screenshot of sourcegraph interface demonstrating Syntactic indexing toggle enabled for a code graph policy](https://storage.googleapis.com/sourcegraph-assets/Docs/syntactic-indexing-policy.png) Syntactic indexing can only be enabled if the policy type is set to HEAD – only indexing the tip of default branch on each repository. You may choose to apply this policy only to selected repositories, although the recommendation is to enable it instance-wide. ## Supported languages Syntactic code navigation requires language-specific implementations, and we currently support the following languages: - [Java](https://www.java.com/en/) - [Go](https://go.dev/) - [Scala](https://scala-lang.org) - [C#](https://learn.microsoft.com/en-us/dotnet/csharp/) - [C++](https://isocpp.org/) We are constantly looking to expand the list of supported languages, with Python, Typescript/Javascript, and C planned. If you are interested in syntactic indexing for these or any other languages, please contact your Sourcegraph account representative. ## Comparison with Precise and Search-based Syntactic code navigation falls between Precise and Search-based code navigation both in terms of quality of navigation, and in terms of effort to maintain for different languages: ![Comparison chart showing where different navigation modes are located in terms of effort to maintain and quality of results](https://storage.googleapis.com/sourcegraph-assets/Docs/code-navigation-levels-comparison.png) As the indexing does not involve build tools or actual compilers, it can be performed in isolated environment, with no network access and no configuration, unlike Precise. # Search-based Code Navigation Supported on [Enterprise](/pricing/enterprise) plans. Available via VS Code and JetBrains editor extensions and the Web.

Learn and understand about Search-based Code Navigation.

Sourcegraph comes with a default built-in code navigation provided by search-based heuristics. It works out of the box with all of the most popular programming languages. ## How does it work? Search-based Code Navigation provides 3-core navigation features: - **Jump to definition**: Performs a [symbol search](/code-search/features#symbol-search). - **Hover documentation**: First, finds the definition. Then, extracts documentation from comments near the definition. - **Find references**: Performs a case-sensitive word-boundary cross-repository [plain text search](/code-search/features#powerful-flexible-queries) for the given symbol Search-based Code Navigation also filters results by file extension and by imports at the top of the file for some languages. ## What configuration settings can I apply? The symbols container recognizes these environment variables: | **Env Vars** | **Deafult** | **Details** | | ---------------------------- | ------------------------------------- | ------------------------------------------------------------------------------------------------- | | `CTAGS_COMMAND` | `universal-ctags` | Ctags command (should point to universal-ctags executable compiled with JSON and seccomp support) | | `CTAGS_PATTERN_LENGTH_LIMIT` | `250` | The maximum length of the patterns output by ctags | | `LOG_CTAGS_ERRORS` | `false` | Log ctags errors | | `SANITY_CHECK` | `false` | Check that go-sqlite3 works then exit 0 if it's ok or 1 if not | | `SYMBOLS_CACHE_DIR` | `/tmp/symbols-cache` | Directory in which to store cached symbols | | `SYMBOLS_CACHE_SIZE_MB` | `100000` | Maximum size of the disk cache (in megabytes) | | `CTAGS_PROCESSES` | `strconv.Itoa(runtime.GOMAXPROCS(0))` | Number of concurrent parser processes to run | | `REQUEST_BUFFER_SIZE` | `8192` | Maximum size of buffered parser request channel | | `PROCESSING_TIMEOUT` | `2 hrs` | Maximum time to spend processing a repository | | `MAX_TOTAL_PATHS_LENGTH` | `100000` | Maximum sum of lengths of all paths in a single call to git archive | | `USE_ROCKSKIP` | `false` | Enables [Rockskip](/code-search/code-navigation/rockskip) for fast symbol searches and search-based code navigation on repositories specified in `ROCKSKIP_REPOS`, or respositories over `ROCKSKIP_MIN_REPO_SIZE_MB` in size | | `ROCKSKIP_REPOS` | N/A | In combination with `USE_ROCKSKIP=true` this specifies a comma-separated list of repositories to index using [Rockskip](/code-search/code-navigation/rockskip) | | `ROCKSKIP_MIN_REPO_SIZE_MB` | N/A | In combination with `USE_ROCKSKIP=true` all repos that are at least this big will be indexed using Rockskip | | `MAX_CONCURRENTLY_INDEXING` | 4 | Maximum number of repositories being indexed at a time by [Rockskip](/code-search/code-navigation/rockskip) (also limits ctags processes) | The default values for these environment variables come from [`config.go`](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/eea895ae1a8acef08370a5cc6f24bdc7c66cb4ed/cmd/symbols/config.go#L42-L59). To enable precise code navigation for your repository, see our [docs here](/code-search/code-navigation/precise_code_navigation#setting-up-code-navigation-for-your-codebase). ## More resources
# Rockskip: fast symbol sidebar and search-based code navigation on monorepos This feature is in Beta stage. Rockskip is an alternative symbol indexing and query engine for the symbol service intended to improve performance of the symbol sidebar and search-based code navigation on big monorepos. It was added in Sourcegraph 3.38. ## When should I use Rockskip? ![still processing symbols error](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/symbol-sidebar-timeout.png) ![hover popover spinner](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/hover-popover-spinner.png) If you regularly see the above error or slow hover popovers (when not using precise navigation), it probably means that the default implementation (which copies SQLite DBs for each commit) is not fast enough and that Rockskip might help. A very rough way to gauge if Rockskip will help is if your repository has a 2GB+ `.git` directory, 50K+ commits, or 50K+ files in the working tree. You can always try Rockskip for a while and if it doesn't help then you can disable it. ## How do I enable Rockskip? **Step 1:** Set environment variables on the `symbols` container: For Docker Compose: ```yaml services: symbols-0: environment: # Enables Rockskip - USE_ROCKSKIP=true # Uses Rockskip for all repositories over 1GB - ROCKSKIP_MIN_REPO_SIZE_MB=1000 ``` For Helm: ```yaml # overrides.yaml symbols: env: # Enables Rockskip USE_ROCKSKIP: value: "true" # Uses Rockskip for all repositories over 1GB ROCKSKIP_MIN_REPO_SIZE_MB: value: "1000" ``` For Kubernetes: ```yaml # base/symbols/symbols.Deployment.yaml spec: template: spec: containers: - name: symbols env: # Enables Rockskip - name: USE_ROCKSKIP value: "true" # Uses Rockskip for all repositories over 1GB - name: ROCKSKIP_MIN_REPO_SIZE_MB value: "1000" ``` For all deployments, make sure that: - The `symbols` service has access to the codeintel DB - The `symbols` service has the environment variables set - The `codeintel-db` has a few extra GB of RAM **Step 2:** Kick off indexing 1. Visit your repository in the Sourcegraph UI 1. Click on the branch selector, click **Commits**, and select the second most recent commit (this avoids routing the request to Zoekt) 1. Open the symbols sidebar to kick off indexing. You can expect to see a loading spinner for 5s then an error message saying that indexing is in progress with the estimated time remaining. **Step 3:** Wait for indexing to complete. You can check the status as before by refreshing the page, opening the symbols sidebar, and looking at the error message. If you are interested in more technical details about the status, see the [instructions below](#how-do-i-check-the-indexing-status). **Step 4:** Open the symbols sidebar again and the symbols should appear quickly. Hover popovers and jump-to-definition via search-based code navigation should also respond quickly. That's it! New commits will be indexed automatically when users visit them. ## How long does indexing take? The initial indexing takes roughly 4 hours per GB of the `.git` directory (you can check the size with `du -sch .git`). Once the full repository has been indexed, indexing new commits takes less than 1 second most of the time. ## What resources does Rockskip use? Rockskip stores all data in Postgres, and the tables it creates use roughly 3x as much space as your `.git` directory, so make sure your Postgres instance has enough free disk. Rockskip indexes every symbol in the entire history of your repository and makes heavy use of Postgres indexes to make all kinds of queries fast, including: path prefix queries, regex queries with trigram index optimization, file extension queries, and the internal commit visibility queries. Rockskip is completely single-threaded when indexing a repository, but multiple repositories can be indexed at a time. The concurrency is limited by `MAX_CONCURRENTLY_INDEXING`, which defaults to 4. Rockskip heavily relies on gitserver for data. Rockskip issues very long-running `git log` commands, as well as many `git archive` calls. ## How do I check the indexing status? The easiest way to check the status of a single repository is to open the symbols sidebar and wait 5s for an error message to appear with the estimated time remaining. For more info, the symbols container responds to GET requests on the `localhost:3184/status` endpoint with the following info: - Repository count - Size of the symbols table in Postgres - Most recently searched repositories - List of in-flight indexing and search requests For Kubernetes, find the symbols pod and `exec` a `curl` command in it: ``` yaml $ kubectl get pods | grep symbols symbols-5ff7c67b57-mr5h4 $ kubectl exec -ti symbols-5ff7c67b57-mr5h4 -- curl localhost:3184/status This is the symbols service status page. Number of repositories: 1 Size of symbols table: 3253 MB Most recently searched repositories (at most 5 shown) 2022-03-11 05:48:58.890765 +0000 UTC github.com/sgtest/megarepo Here are all in-flight requests: indexing github.com/sgtest/megarepo@14a3d9849ba587d667efbc542cf0c7cd106c3e72 progress 9.53% (indexed 49151 of 515574 commits), 36h55m18.227079912s remaining Tasks (14006.77s total, current AppendHop+): AppendHop+ 44.76% 49152x, InsertSymbol 18.67% 1997101x, AppendHop- 12.94% 49151x, UpdateSymbolHops 7.78% 825380x, parse 4.01% 369401x, GetCommitByHash 2.73% 515574x, get hops 2.39% 49152x, ArchiveEach 2.26% 98302x, GetSymbol 1.83% 325351x, CommitTx 1.26% 49151x, DeleteRedundant 0.79% 49151x, InsertCommit 0.30% 49152x, Log 0.28% 1x, RevList 0.00% 1x, iLock 0.00% 1x, idle 0.00% 1x, holding iLock ``` In this example you can see there's 1 repository and the symbols service has indexed 9% of all commits with an ETA of 36H from now. There's also a breakdown of tasks that are part of Rockskip's internal workings mostly for Sourcegraph engineers, so you can ignore that. ## When is indexing triggered? Indexing is triggered upon opening the symbol sidebar or hovering on a token. Both of those UI features hit the same `type:symbol` search API which is serviced under the hood by Rockskip. Rockskip indexes the new commits since the previously indexed commit, so if it's been a long time since a user last opened the symbol sidebar then Rockskip will take longer to process before it can service queries. Simply opening the symbol sidebar more frequently (e.g. via having more users on the instance) will decrease the probability of seeing the still-processing message. ## How does it work? For a deeper dive into the index and query structures, check out the [explanatory RFC](https://docs.google.com/document/d/1sDDpZaWdGtIaiNLNB8QsLwHTvH10fhEKpEa4qcog5vg/edit?usp=sharing). ## Limitations - Rockskip does not support file paths that are not encoded in UTF-8. Consequently, any symbols in files with non-UTF-8 encoded paths will not be indexed. # Configuring Maven to Use a Private Repository for Auto-Indexing Configuring Maven to use a private repository, such as Nexus or Artifactory, with Sourcegraph is essential when dependencies are retrieved from private repositories that require authentication (as opposed to public repositories such as Maven Central). This guide covers steps to set up Maven for [scip-java](https://sourcegraph.github.io/scip-java/) indexing with a private repository, ensuring secure and consistent dependency resolution. ## Testing the Configuration on a Single Repository To test and validate the Maven configuration, modify a single repository’s auto-indexing settings to include a custom settings.xml file. Refer to (Maven's official docs)[https://maven.apache.org/settings.html#quick-overview] for an overview of how this file is used to configure Maven repositories and other settings. ### Add Custom Index Job Configuration - Access the repository’s index settings in Sourcegraph and open the “Raw” configuration panel. - Insert the following configuration: ```json { "steps": [], "local_steps": [ "mkdir -p ~/.m2", "echo ' repo $ARTIFACTORY_USER $ARTIFACTORY_PASSWORD ' > ~/.m2/settings.xml" ], "root": "", "indexer": "sourcegraph/scip-java:latest", "indexer_args": [ "scip-java", "index", "--build-tool=auto" ], "outfile": "index.scip", "requestedEnvVars": [ "ARTIFACTORY_USER", "ARTIFACTORY_PASSWORD" ] } ``` ### Set Up Executor Secrets Before triggering the indexing process, make sure the environment variables `$ARTIFACTORY_USER` and `$ARTIFACTORY_PASSWORD` are created as [Executor Secrets](https://sourcegraph.com/docs/admin/executors/executor_secrets). ### Trigger Indexing After configuring the repository, navigate to the "Precise Indexes" tab and click "Enqueue" to start indexing. ## Automating the Configuration Across All Repositories After verifying the configuration on a single repository, you can automate the setup across all repositories by modifying the inference configuration using a Lua script. ### Create or Update the Lua Script Navigate to `site-admin -> Code Graph -> Inference`, and replace or add the following Lua script for [scip-java](https://sourcegraph.github.io/scip-java/) indexing with Maven’s `settings.xml` setup: ```lua local path = require("path") local pattern = require("sg.autoindex.patterns") local recognizer = require("sg.autoindex.recognizer") local patterns = require "internal_patterns" local new_steps = { 'mkdir -p ~/.m2', [[echo "repo$ARTIFACTORY_USER$ARTIFACTORY_PASSWORD" > ~/.m2/settings.xml]] } local new_requested_env_variables = {'ARTIFACTORY_USER', 'ARTIFACTORY_PASSWORD'} local java_indexer = require("sg.autoindex.indexes").get "java" local custom_java_recognizer = recognizer.new_path_recognizer { patterns = { pattern.new_path_basename("pom.xml"), pattern.new_path_basename("build.gradle"), pattern.new_path_basename("build.gradle.kts"), }, generate = function(api, paths) api:register({ local_steps = new_steps, requested_envvars = new_requested_env_variables, root = path.dirname(paths[1]), outfile = "index.scip", indexer = java_indexer, indexer_args = { "scip-java", "index", "--build-tool=auto" } }) end } return require("sg.autoindex.config").new({ ["custom.java"] = custom_java_recognizer, ["sg.java"] = false }) ``` ### Verify the Configuration Once the Lua script is applied, you can verify that the configuration works by using the **"Preview results"** button in the Lua script editor under the "Inference Configuration" section. This will display the inferred index jobs for your repositories, showing details such as the root directory, indexer, indexer arguments, and environment variables used for each job. If everything is configured correctly, the dependencies will be pulled from the specified private repository without issues. # Precise Code Navigation Supported on [Enterprise](/pricing/enterprise) plans. Available via the Web app.

Learn and understand about Precise Code Navigation.

Precise Code Navigation is an opt-in feature that is enabled from your admin settings and requires you to upload indexes for each repository to your Sourcegraph instance. Once setup is complete on Sourcegraph, precise code navigation is available for use across popular development tools: - On the Sourcegraph web UI - On code files on your code host, via [integrations](/integration/) - On diffs in your code review tool, via integrations - Via the [Sourcegraph API](/api/graphql) Sourcegraph automatically uses Precise Code Navigation whenever available, and Search-based Code Navigation is used as a fallback when precise navigation is not available. Precise code navigation relies on the open source [SCIP Code Intelligence Protocol](https://github.com/sourcegraph/scip), which is a language-agnostic protocol for indexing source code. ## Setting up code navigation for your codebase There are several options for setting up precise code navigation listed below. However, we always recommend you start by manually indexing your repo locally using the [approriate indexer](/code-search/code-navigation/writing_an_indexer#quick-reference) for your language. Code and build systems can vary by project and ensuring you can first succesfully run the indexer locally leads to a smoother experience since it is vastly easier to debug and iterate on any issues locally before trying to do so in CI/CD or in Auto-Indexing. 1. **Manual indexing**. Index a repository and upload it to your Sourcegraph instance: - [Index a Go repository](/code_navigation/how-to/index_a_go_repository#manual-indexing) - [Index a TypeScript or JavaScript repository](/code_navigation/how-to/index_a_typescript_and_javascript_repository#manual-indexing) - [Index a Java, Scala, or Kotlin repository](https://sourcegraph.github.io/scip-java/docs/getting-started.html) - [Index a Python repository](https://sourcegraph.com/github.com/sourcegraph/scip-python) - [Index a Ruby repository](https://sourcegraph.com/github.com/sourcegraph/scip-ruby) 2. [**Automate indexing via CI**](/code-search/code-navigation/how-to/adding_scip_to_workflows): Add indexing and uploading to your CI setup. 3. [**Auto-indexing**](/code-search/code-navigation/auto_indexing#enable-auto-indexing): Sourcegraph will automatically index your repositories and enable precise code navigation for them. ## Supported languages and indexers Precise Code Navigation requires language-specific indexes to be generated and uploaded to your Sourcegraph instance. We currently have precise code navigation support for the languages below. See the [indexers page](/code-search/code-navigation/writing_an_indexer) for a detailed breakdown of each indexer's status. | Language | Indexer | Status | | ---------------------- | --------------------------------------------------------------------------------- | --------------------- | | Go | [scip-go](https://sourcegraph.com/github.com/sourcegraph/scip-go) | 🟢 Generally available | | TypeScript, JavaScript | [scip-typescript](https://sourcegraph.com/github.com/sourcegraph/scip-typescript) | 🟢 Generally available | | C, C++, CUDA | [scip-clang](https://sourcegraph.com/github.com/sourcegraph/scip-clang) | 🟢 Generally available | | Java, Kotlin, Scala | [scip-java](https://sourcegraph.com/github.com/sourcegraph/scip-java) | 🟢 Generally available | | Rust | [rust-analyzer](https://sourcegraph.com/github.com/rust-lang/rust-analyzer) | 🟢 Generally available | | Python | [scip-python](https://sourcegraph.com/github.com/sourcegraph/scip-python) | 🟢 Generally available | | Ruby | [scip-ruby](https://sourcegraph.com/github.com/sourcegraph/scip-ruby) | 🟢 Generally available | | C#, Visual Basic | [scip-dotnet](https://github.com/sourcegraph/scip-dotnet) | 🟢 Generally available | The easiest way to configure precise code navigation is with [auto-indexing](/code-search/code-navigation/auto_indexing). This feature uses [Sourcegraph executors](/admin/executors/) to automatically create indexes for the code, keeping precise code navigation available and up-to-date. ## Precise navigation examples The following repositories have precise code navigation enabled: | **Programming Language** | **Repos** | | ------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Go | - [`kubernetes/kubernetes`](https://sourcegraph.com/github.com/kubernetes/kubernetes@master/-/blob/cmd/cloud-controller-manager/main.go?L45:2&popover=pinned)
- [`gohugoio/hugo`](https://sourcegraph.com/github.com/gohugoio/hugo@master/-/blob/common/hugo/hugo.go?L63:15&popover=pinned)
- [`gin-gonic/gin`](https://sourcegraph.com/github.com/gin-gonic/gin@master/-/blob/routergroup.go?L33:6&popover=pinned) | | Java | - [`sourcegraph/jetbrains`](https://sourcegraph.com/github.com/sourcegraph/jetbrains/-/blob/src/main/java/com/sourcegraph/cody/CodyActionGroup.java?L13) | | Scala | - [`neandertech/langoustine`](https://sourcegraph.com/github.com/neandertech/langoustine/-/blob/modules/lsp/src/main/scala/Communicate.scala?L28) | | Kotlin | - [`sourcegraph/jetbrains`](https://sourcegraph.com/github.com/sourcegraph/jetbrains/-/blob/src/main/kotlin/com/sourcegraph/cody/agent/CodyAgent.kt?L42) | | Python | - [`pipecat-ai/pipecat-flows`](https://sourcegraph.com/github.com/pipecat-ai/pipecat-flows/-/blob/src/pipecat_flows/actions.py?L38) | | TypeScript | - [`vuejs/vue`](https://sourcegraph.com/github.com/vuejs/vue@main/-/blob/src/core/observer/index.ts?L68:3&popover=pinned) | | Ruby | - [`Homebrew/brew`](https://sourcegraph.com/github.com/Homebrew/brew@master/-/blob/Library/Homebrew/utils/bottles.rb?L18:18&popover=pinned) | | Rust | - [`rust-lang/cargo`](https://sourcegraph.com/github.com/rust-lang/cargo/-/blob/src/cargo/core/compiler/compilation.rs?L15:12&popover=pinned#tab=references)
- [`rust-lang/rustlings`](https://sourcegraph.com/github.com/rust-lang/rustlings@main/-/blob/src/dev.rs?L10) | | C, C++ | - [`sourcegraph/cxx-precise-examples`](https://sourcegraph.com/github.com/sourcegraph/cxx-precise-examples/-/blob/piecewise_monorepo/arithmetic/src/multiplication.cpp?L3) | ## More resources
# Auto-indexing inference configuration reference This feature is in beta for self-hosted customers. This document details how a site administrator can supply a Lua script to customize the way [Sourcegraph detects precise code intelligence indexing jobs from repository contents](/code-search/code-navigation/explanations/auto_indexing_inference). By default, Sourcegraph will attempt to infer index jobs for the following languages: - [`Go`](/code-search/code-navigation/explanations/auto_indexing_inference#go) - [`Java`/`Scala`/`Kotlin`](/code-search/code-navigation/explanations/auto_indexing_inference#java) - `Python` - `Ruby` - [`Rust`](/code-search/code-navigation/explanations/auto_indexing_inference#rust) - [`TypeScript`/`JavaScript`](/code-search/code-navigation/explanations/auto_indexing_inference#typescript) Inference logic can be disabled or altered in the case when the target repositories do not conform to a pattern that the Sourcegraph default inference logic recognizes. Inference logic is controlled by a **Lua override script** that can be supplied in the UI under `Admin > Code graph > Inference`. While the change is self-service, Sourcegraph support is more than happy to help write custom behaviors with you. Do not hesitate to contact us to get the inference logic behaving how you would expect for your repositories. ## Example The **Lua override script** ultimately must return an _auto-indexing config object_. A configuration that neither disables or adds new recognizers does not change the default inference behavior. ```lua return require("sg.autoindex.config").new({ -- Empty configuration (see below for usage) }) ``` To **disable** default behaviors, you can re-assign a recognizer value to `false`. Each of the built-in recognizers are prefixed with `sg.` (and are the only ones allowed to be). ```lua return require("sg.autoindex.config").new({ -- Disable default Python inference ["sg.python"] = false }) ``` To **add** additional behaviors, you can create and register a new **recognizer**. A recognizer is an interface that requests some set of files from a repository, and returns a set of auto-indexing job configurations that could produce a precise code intelligence index. A _path recognizer_ is a concrete recognizer that advertises a set of path _globs_ it is interested in, then invokes its `generate` function with matching paths from a repository. In the following, all files matching `Snek.module` (`Snek.module`, `proj/Snek.module`, `proj/sub/Snek.module`, etc) are passed to a call to `generate` (if non-empty). The generate function will then return a list of indexing job descriptions. The [guide for auto-indexing jobs configuration](/code-search/code-navigation/auto_indexing_configuration#keys-1) gives detailed descriptions on the fields of this object. The ordering of paths and limits are defined in the [Ordering guarantees and limits](#ordering-guarantees-and-limits) section. ```lua local path = require("path") local pattern = require("sg.autoindex.patterns") local recognizer = require("sg.autoindex.recognizer") local snek_recognizer = recognizer.new_path_recognizer { patterns = { -- Look for Snek.module files -- (would match Snek.module; proj/Snek.module, proj/sub/Snek.module, etc) pattern.new_path_basename("Snek.module"), -- Ignore any files in test or vendor directories pattern.new_path_exclude( pattern.new_path_segment("test"), pattern.new_path_segment("vendor") ), }, -- Called with list of matching Snek.module files generate = function(_, paths) local jobs = {} for i = 1, #paths do -- Create indexing job description for each matching file table.insert(jobs, { indexer = "acme/snek:latest", -- Run this indexer... root = path.dirname(paths[i]), -- ...in this directory local_steps = {"snekpm install"}, -- Install dependencies indexer_args = {"snek", "index", ".", "--output", "index.scip"}, outfile = "index.scip", }) end return jobs end } return require("sg.autoindex.config").new({ -- Register new recognizer ["acme.snek"] = snek_recognizer, }) ``` ## Available libraries There are a number of specific and general-purpose Lua libraries made accessible via the built-in `require`. The type signatures for the functions below use the following syntax: - `(A1, ..., An) -> R`: Function type with arguments of type `A1, ..., An` and return type `R`. - `array[A]`: Table with indexes 1 to N of elements of type `A`. - `table[K, V]`: Table with keys of type `K` and values of type `V`. - `A | B`: Union type (includes values of type `A` and type `B`). - `A...`: Variadic (0 or more values of A, without being wrapped in a table). - `"mystring"`: Literal string type with only `"mystring"` as the allowed value. - `{K1: V1, K2: V2, ...}`: Heterogenous table (object) with a key of type `K1` mapping to a value of type `V1` etc. - `void`: no value returned from function ### `sg.autoindex.recognizer` This auto-indexing-specific library defines the following two functions. - `new_path_recognizer` creates a `Recognizer` from a config object containing `patterns` and `generate` fields. See the [example](#example) above for basic usage. - Type: ``` ({ -- List of patterns to match against paths in the repository "patterns": array[pattern], -- List of patterns to match against paths in the repository -- for getting contents (see contents_by_path below) "patterns_for_content": array[pattern], -- Callback function invoked with paths requested by patterns above -- for creating index jobs "generate": ( registration_api, -- List of paths obtained from 'patterns' and -- 'patterns_for_content' combined. paths: array[string], -- Table mapping paths to contents for paths matched by -- 'patterns_for_content' contents_by_path: table[string, string] ) -> array[index_job], }) -> recognizer ``` where `index_job` is an object with the following shape: ``` index_job = { -- Docker image for the indexer "indexer": string, -- Working directory for invoking the indexer "root": string, -- Preparatory steps to run before invoking the indexer -- such as installing dependencies "steps": array[{ -- Working directory for this step "root": string, -- Docker image to use for this step "image": string, -- List of commands to run inside the Docker image "commands": array[string] }], -- List of commands to run inside the indexer image at "root" -- before invoking the indexer, such as installing dependencies. "local_steps": array[string], -- Command-line invocation for the indexer "indexer_args": array[string], -- Path to the index generated by the indexer "outfile": string, -- Names of necessary environment variables. These are -- made accessible to steps, local_steps, and the -- indexer_args command. -- -- These are generally used for passing secrets. "requested_envvars": array[string], } ``` For installing dependencies, if the indexer image contains the relevant package manager(s), then it is simpler to install dependencies using `local_steps`. Otherwise, the `steps` field allows more customizability. - `new_fallback_recognizer` creates a `recognizer` from an ordered list of `recognizer`s. Each `recognizer` is called sequentially, until one of them emits non-empty results. - Type: `(array[recognizer]) -> recognizer` The `registration_api` object has the following API: - `register` which queues a `recognizer` to be run at a later stage. This makes it possible to add more recognizers dynamically, such as based on whether specific configuration files were found or not. - Type: `(recognizer) -> void` ### `sg.autoindex.patterns` This auto-indexing-specific library defines the following four path pattern constructors. - `new_path_literal(fullpath)` creates a `pattern` that matches an exact filepath. - Type: `(string) -> pattern` - `new_path_segment(segment)` creates a `pattern` that matches a directory name. - Type: `(string) -> pattern` - `new_path_basename(basename)` creates a `pattern` that matches a basename exactly. - Type: `(string) -> pattern` - `new_path_extension(ext_no_leading_dot)` creates a `pattern` that matches files with a given extension. - Type: `(string) -> pattern` This library also defines the following two pattern collection constructors. - `new_path_combine(patterns)` creates a pattern collection object (to be used with [recognizers](#sg-autoindex-recognizers)) from the given set of path `pattern`s. - Type: `((pattern | array[pattern])...) -> pattern` - `new_path_exclude(patterns)` creates a new _inverted_ pattern collection object. Paths matching these `pattern`s are filtered out from the set of matching filepaths given to a recognizer's `generate` function. - Type: `((pattern | array[pattern])...) -> pattern` ### `path` This library defines the following utility functions: - `ancestors(path)` returns a list `{dirname(path), dirname(dirname(path)), ...}`. The last element in the list will be an empty string. - Type: `(string) -> array[string]` - `basename(path)` returns the basename of the given path as defined by Go's [filepath.Base](https://pkg.go.dev/path/filepath#Base). - Type: `(string) -> string` - `dirname(path)` returns the dirname of the given path as defined by Go's [filepath.Dir](https://pkg.go.dev/path/filepath#Dir), except that it (1) returns an empty path instead of `"."` if the path is empty and (2) removes a leading `/` if present. - Type: `string -> string` - `join(path1, path2)` returns a filepath created by joining the given path segments via filepath separator. - Type: `(string, string) -> string` - `split(path)` is a convenience function that returns `dirname(path), basename(path)`. - Type: `(string) -> string, string` ### `json` This library defines the following two JSON utility functions: - `encode(val)` returns a JSON-ified version of the given Lua object. - `decode(json)` returns a Lua table representation of the given JSON text. ### `fun` [Lua Functional](https://github.com/luafun/luafun/tree/cb6a7e25d4b55d9578fd371d1474b00e47bd29f3#lua-functional) is a high-performance functional programming library accessible via `local fun = require("fun")`. This library has a number of functional utilities to help make recognizer code a bit more expressive. ## Ordering guarantees and limits Sourcegraph enforces several limits to avoid inference timeouts and ever-growing auto-indexing queues. These limits apply for a single round of inference for a single repository, combined across all recognizers, including any implicitly included Sourcegraph recognizers. Limit | Default value :-----|-------------: The number of auto-indexing jobs inferred | 100 The number of total paths passed to the inference script's `generate` functions as the second argument `paths` | 500 The number of total paths with contents passed to the inference script's `generate` functions as the third argument `contents_by_paths` | 100 Maximum size limit for file contents, in bytes | 1 MiB Please reach out to Sourcegraph support if you'd like to change these limits. Auto-indexing jobs and paths are first ranked based on the criteria described below. If the number of jobs and/or paths exceeds the limits above, lower ranked items are discarded. - For auto-indexing jobs, ranking is done based on the following: - Descending order of indexer frequency (total number of inferred jobs with the same `indexer` field). - Ascending lexicographic ordering of `indexer`. - Descending order of number of path components for `root`. Shallower roots are preferrred over deeper ones as they are more likely to cover more code. - Ascending lexicographic ordering of `root` paths. - For paths, ranking happens in the following order: - Paths for which the contents are requested are ranked higher. - Paths with fewer components are ranked higher. - Otherwise, lexicographic ordering of paths is used. # Code Navigation

Learn how to navigate your code and understand its dependencies with high precision.

Code Navigation helps you quickly understand your code, its dependencies, and symbols within the Sourcegraph file view while making it easier to move through your codebase via: - Onboarding to codebases faster with cross-repository code navigation features like [Go to definition](/code-search/code-navigation/features#go-to-definition) and [Find references](/code-search/code-navigation/features#find-references) - Providing complete precise reviews, getting up to speed on unfamiliar code, and determining the impact of code changes with the confidence of compiler-accurate code navigation - Determining the root causes quickly with precise code navigation that tracks references across repositories and package dependencies ## Quicklinks ## Features Code Navigation helps you with the following tasks: | **Feature** | **Description** | | -------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [Popover](/code_navigation/explanations/features#popover) | Quickly view a symbol's type signature and documentation without switching to another source file | | [Go to definition](/code_navigation/explanations/features#go-to-definition) | Click the button or symbol name, navigates you to the symbol's definition | | [Find references](/code_navigation/explanations/features#find-references) | Selecting it in the popover lists all references, definitions, and implementations at the bottom, including precise and search-based results | | [Find implementations](/code_navigation/explanations/features#find-implementations) | Click to go to a symbol's interface definition or, at the interface, see all implementations across repositories, including interfaces implemented by a struct | | [Perform an action](/code-search/code-navigation/features#perform-an-action) | When browsing code, you can perform a couple of actions like open in code host, raw download and view blame. | Read and learn more about [Code Navigation features here →](/code-search/code-navigation/features) ## Code Navigation types There are two types of Code Navigation that Sourcegraph supports: - [Search-based Code Navigation](/code_navigation/explanations/search_based_code_navigation): Works out of the box with most popular programming languages, powered by Sourcegraph's code search. It uses a mix of text search and syntax-level heuristics (no language-level semantic information) for fast, performant searches across large code bases. - [Precise Code Navigation](/code_navigation/explanations/precise_code_navigation): Uses compile-time information to provide users with accurate cross-repository navigation experience across the entire code base.
# Code Navigation Features

Learn and understand more about Sourcegraph's Code Navigation features and core functionality.

Using our [integrations](/integration/), all code navigation features are available everywhere you read code. This includes browsers and GitHub pull requests. ![code-navigation-features](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/extension-example.gif) ## Popover Popovers allow you to quickly glance at the type signature and accompanying documentation of a symbol definition without having to context switch to another source file (which may or may not be available while browsing code). ![popover](https://storage.googleapis.com/sourcegraph-assets/Docs/popover.png) ## Go to definition When you click on the **Go to definition** button in the popover or click on a symbol's name (in the sidebar or code view), you will be navigated directly to the definition of the symbol. ![go-to-definition](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/go-to-def.gif) ## Find references When you select **Find references** from the popover, a panel at the bottom of the page lists all references, definitions, and implementations found for both precise and search-based results (from search heuristics). ![find-references](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/find-references.gif) When a particular token returns a large number of references, we truncate the results to < 500 to optimize for browser loading speed. We are planning to improve this in the future with the ability to view it as a search so that users can utilize the powerful filtering of Sourcegraph's search to find the references they are looking for. ## Dependency navigation This feature is in Beta stage and is not available for other language ecosystems. Dependency navigation enables **Find references** and **Go to definition** to show usages across multiple repositories, including transitive dependencies. For example, the animation below demonstrates how to trigger **Find references** in the `github.com/Netflix/Hystrix` repository and navigate to results in the Java standard library (JDK). ![dependency-navigation](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/dependency-nav.gif) The instructions to set up dependency navigation require you to set up auto-indexing. For more information on how to set up auto-indexing, please refer to our [auto-indexing documentation](/code-search/code-navigation/auto_indexing). ## Find implementations If precise code navigation is enabled for your repositories, you can click **Find Implementations** to navigate to a symbol's interface definition. If you're at the interface definition itself, clicking on **Find Implementations** will show all the places where the interface is being implemented, allowing you to explore how other users across repositories use it. It can also show which interfaces a struct implements. ![find-implementations](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/find-impl.gif) Read [here](/code-search/code-navigation/writing_an_indexer#quick-reference) for an overview of which languages support this feature. ## Perform an Action Code Search allows you to harness the power of Search and quickly find, discover, and understand code. However, the end result isn't always to view the results of a search query. Code Search empowers you to perform an action given a set of results. ![Code Search Actions](https://storage.googleapis.com/sourcegraph-assets/Docs/actions/code-search-actions.png) When browsing code, you can perform the following actions: ### Ask Cody Cody is a free and open-core AI coding assistant that writes, fixes, and maintains your code and is available on Sourcegraph. It allows you to ask natural language questions about your code and receive intelligent suggestions and explanations. When browsing code on Sourcegraph, you can ask Cody a question by clicking the **Ask Cody** button above the file you are viewing. This will open a chat interface where you can have a conversation with Cody about the code you are viewing. Having Cody integrated directly into Sourcegraph code search makes it fast and easy to get AI-powered insights as you explore code. ### Open in Editor When viewing a file in Sourcegraph, you can open it in your editor of choice to edit the file. This allows you to seamlessly transition from browsing code in Sourcegraph to editing code in your preferred code editor. When you click the Open in Editor button, Sourcegraph will detect which editor you have configured and open the file in a new tab in that editor. If you have not configured a default editor, Sourcegraph will prompt you to select an editor before opening the file. You can read more about this [here](/integration/open_in_editor). ### View History The View History action allows you to see the full commit history for a file directly in Sourcegraph. When you click this button, a sidebar will open showing every commit that touched the file, ordered chronologically from newest to oldest. For each commit, you can view the following: - Commit message - Author - Date - Commit SHA ![History action](https://storage.googleapis.com/sourcegraph-assets/Docs/actions/history-action.png) You can click on any commit to view the full diff and see exactly what changed in that particular version of the file. Exploring the history of a file helps you understand how it evolved over time and why certain changes were made. This additional context can be invaluable when trying to modify or debug code. The file history integrates tightly with other Sourcegraph navigation features. You can seamlessly transition from browsing history to blaming lines or searching for references. ### Blame The Blame action shows annotation on each line of a file indicating the last commit that modified the line and who the author was. When you click this button on a file, blame information will be displayed inline. This allows you to easily see the history of every line, without having to explore the full file history. Blame tells you: * The commit that last modified each line * The author who made the change * The commit message * The timestamp of when it was changed ![Blame action](https://storage.googleapis.com/sourcegraph-assets/Docs/actions/blame-action.png) Using blame makes it easy to answer questions like: * Who wrote this line of code originally? * When was this method added? * What changes were made in this commit? You can click on any commit in the blame view to explore that snapshot of the file and see the full diff. Blame data is essential when modifying or debugging existing code in a shared codebase. It helps identify who to talk to when you have questions about particular sections of code. ### Open in Code host The Open in CodeHost button allows you to quickly view the file in its native code host. When you click this button, the file will open in a new browser tab taking you directly to that file on the code host website. For example, if you are viewing a file from a GitHub repository in Sourcegraph, clicking Open in CodeHost will open `GitHub.com` in a new tab with that file displayed. ### Copy Link The Copy Link action allows you to easily copy a permanent link to the exact line range you are viewing in Sourcegraph. When you click this button, the link is copied to your clipboard. You can then paste the link in any text field or document. ![Copy Link action](https://storage.googleapis.com/sourcegraph-assets/Docs/actions/copy-action.gif) ### Raw download The Raw download action allows you to download the raw file contents displayed in Sourcegraph. When viewing code, click the Raw download button to save the file to your machine. ### Line Wrapping The Line Wrap toggle allows you to wrap long lines that extend past the width of the code viewer. When enabled, any line longer than the window width will wrap to the next line, similar to text in a document. This makes it easier to read code snippets with very long lines without having to scroll horizontally. * When disabled (default), long lines will extend past the edge of the window, requiring horizontal scrolling to see the full line. * When enabled, lines will break to the next line before hitting the window edge. Toggling line wrapping on and off helps optimize code readability depending on personal preference. Wrapped lines are visually indicated with a faint vertical line connecting the split sections. Hovering also reveals the full continuous line. Line wrapping does not modify the actual code contents - it is purely a visual modification. Downloaded files will contain the original non-wrapped lines.
# Code Navigation Environment Variables

These docs list supported environment variables for Code Navigation.

## frontend The following are variables are read from the `frontend` service to control code navigation behavior exposed via the GraphQL API. | **Name** | **Default** | **Description** | | ------------------------------------------------- | ------- | ------------------------------------------------------------- | | `PRECISE_CODE_INTEL_DIAGNOSTICS_COUNT_MIGRATION_BATCH_SIZE` | `1000` | The max no. of document records to migrate at a time. | | `PRECISE_CODE_INTEL_DIAGNOSTICS_COUNT_MIGRATION_BATCH_INTERVAL` | `1s` | The timeout between processing migration batches. | | `PRECISE_CODE_INTEL_DEFINITIONS_COUNT_MIGRATION_BATCH_SIZE` | `1000` | The maximum number of definition records to migrate at once. | | `PRECISE_CODE_INTEL_DEFINITIONS_COUNT_MIGRATION_BATCH_INTERVAL` | `1s` | The timeout between processing migration batches. | | `PRECISE_CODE_INTEL_REFERENCES_COUNT_MIGRATION_BATCH_SIZE` | `1000` | The maximum number of reference records to migrate at a time. | | `PRECISE_CODE_INTEL_REFERENCES_COUNT_MIGRATION_BATCH_INTERVAL` | `1s` | The timeout between processing migration batches. | | `PRECISE_CODE_INTEL_DOCUMENT_COLUMN_SPLIT_MIGRATION_BATCH_SIZE` | `100` | The maximum number of document records to migrate at a time. | | `PRECISE_CODE_INTEL_DOCUMENT_COLUMN_SPLIT_MIGRATION_BATCH_INTERVAL` | `1s` | The timeout between processing migration batches. | | `PRECISE_CODE_INTEL_API_DOCS_SEARCH_MIGRATION_BATCH_SIZE` | `1` | The maximum number of bundles to migrate at a time. | | `PRECISE_CODE_INTEL_API_DOCS_SEARCH_MIGRATION_BATCH_INTERVAL` | `1s` | The timeout between processing migration batches. | | `PRECISE_CODE_INTEL_COMMITTED_AT_MIGRATION_BATCH_SIZE` | `100` | The maximum number of upload records to migrate at a time. | | `PRECISE_CODE_INTEL_COMMITTED_AT_MIGRATION_BATCH_INTERVAL` | `1s` | The timeout between processing migration batches. | | `PRECISE_CODE_INTEL_REFERENCE_COUNT_MIGRATION_BATCH_SIZE` | `100` | The maximum number of upload records to migrate at a time. | | `PRECISE_CODE_INTEL_REFERENCE_COUNT_MIGRATION_BATCH_INTERVAL` | `1s` | The timeout between processing migration batches. | The following settings should be the same for the [`precise-code-intel-worker`](#precise-code-intel-worker) service as well. | **Name** | **Default** | **Description** | | ----------------------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `PRECISE_CODE_INTEL_UPLOAD_BACKEND` | `Blobstore` | The target file service for code graph uploads. S3, GCS, and Blobstore are supported. In older versions of Sourcegraph (before v3.4.2) `Minio` was also a valid value. | | `PRECISE_CODE_INTEL_UPLOAD_MANAGE_BUCKET` | `false` | Whether or not the client should manage the target bucket configuration | | `PRECISE_CODE_INTEL_UPLOAD_BUCKET` | `lsif-uploads` | The name of the bucket to store LSIF uploads in | | `PRECISE_CODE_INTEL_UPLOAD_TTL` | `168h` | The maximum age of an upload before deletion | The following settings should be the same for the [`codeintel-auto-indexing`](#codeintel-auto-indexing) worker task as well. | **Name** | **Default** | **Description** | | ----------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------- | | `PRECISE_CODE_INTEL_AUTO_INDEX_MAXIMUM_REPOSITORIES_INSPECTED_PER_SECOND` | `0` | The maximum number of repositories inspected for auto-indexing per second. Set to zero to disable limit. | | `PRECISE_CODE_INTEL_AUTO_INDEX_MAXIMUM_REPOSITORIES_UPDATED_PER_SECOND` | `0` | The maximum number of repositories cloned or fetched for auto-indexing per second. Set to zero to disable limit. | | `PRECISE_CODE_INTEL_AUTO_INDEX_MAXIMUM_INDEX_JOBS_PER_INFERRED_CONFIGURATION` | `25` | Repositories with a number of inferred auto-index jobs exceeding this threshold will be auto-indexed | ## worker The following are variables are read from the `worker` service to control code graph data behavior run in asynchronous background tasks. ### `codeintel-commitgraph` The following variables influence the behavior of the [`codeintel-commitgraph` worker task](/admin/workers#codeintel-commitgraph). | **Name** | **Default** | **Description** | | ------------------------------------ | ------------------------ | ----------------------------------------------------------------------------------------------------------------------------- | | `PRECISE_CODE_INTEL_MAX_AGE_FOR_NON_STALE_BRANCHES` | `2160h` (about 3 months) | The age after which a branch should be considered stale. Code graph indexes will be evicted from stale branches. | | `PRECISE_CODE_INTEL_MAX_AGE_FOR_NON_STALE_TAGS` | `8760h` (about 1 year) | The age after which a tagged commit should be considered stale. Code graph indexes will be evicted from stale tagged commits. | | `PRECISE_CODE_INTEL_COMMIT_GRAPH_UPDATE_TASK_INTERVAL` | `10s` | The frequency with which to run periodic codeintel commit graph update tasks. | ### `codeintel-auto-indexing` The following variables influence the behavior of the [`codeintel-auto-indexing` worker task](/admin/workers#codeintel-auto-indexing). | **Name** | **Default** | **Description** | | --------------------------------------------- | ------- | ------------------------------------------------------------------------------------------- | | `PRECISE_CODE_INTEL_AUTO_INDEXING_TASK_INTERVAL` | `10m` | The frequency with which to run periodic codeintel auto-indexing tasks. | | `PRECISE_CODE_INTEL_AUTO_INDEXING_REPOSITORY_PROCESS_DELAY` | `24h` | The minimum frequency that the same repository can be considered for auto-index scheduling. | | `PRECISE_CODE_INTEL_AUTO_INDEXING_REPOSITORY_BATCH_SIZE` | `100` | The number of repositories to consider for auto-indexing scheduling at a time. | | `PRECISE_CODE_INTEL_AUTO_INDEXING_POLICY_BATCH_SIZE` | `100` | The number of policies to consider for auto-indexing scheduling at a time. | | `PRECISE_CODE_INTEL_DEPENDENCY_INDEXER_SCHEDULER_POLL_INTERVAL` | `1s` | Interval between queries to the dependency indexing job queue. | | `PRECISE_CODE_INTEL_DEPENDENCY_INDEXER_SCHEDULER_CONCURRENCY` | `1` | The maximum number of dependency graphs that can be processed concurrently. | The following settings should be the same for the [`frontend`](#frontend) service as well. | **Name** | **Default** | **Description** | | ----------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------- | | `PRECISE_CODE_INTEL_AUTO_INDEX_MAXIMUM_REPOSITORIES_INSPECTED_PER_SECOND` | `0` | The maximum number of repositories inspected for auto-indexing per second. Set to zero to disable limit. | | `PRECISE_CODE_INTEL_AUTO_INDEX_MAXIMUM_REPOSITORIES_UPDATED_PER_SECOND` | `0` | The maximum number of repositories cloned or fetched for auto-indexing per second. Set to zero to disable limit. | | `PRECISE_CODE_INTEL_AUTO_INDEX_MAXIMUM_INDEX_JOBS_PER_INFERRED_CONFIGURATION` | `25` | Repositories with a number of inferred auto-index jobs exceeding this threshold will be auto-indexed | ### `codeintel-janitor` The following variables influence the behavior of the [`codeintel-janitor` worker task](/admin/workers#codeintel-janitor). | **Name** | **Default** | **Description** | | | ------------------------------------------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --- | | `PRECISE_CODE_INTEL_UPLOAD_TIMEOUT` | `24h` | The maximum time an upload can be in the 'uploading' state. | | `PRECISE_CODE_INTEL_CLEANUP_TASK_INTERVAL` | `1m` | The frequency with which to run periodic codeintel cleanup tasks. | | `PRECISE_CODE_INTEL_COMMIT_RESOLVER_TASK_INTERVAL` | `10s` | The frequency with which to run the periodic commit resolver task. | | `PRECISE_CODE_INTEL_COMMIT_RESOLVER_MINIMUM_TIME_SINCE_LAST_CHECK` | `24h` | The minimum time the commit resolver will re-check an upload or index record. | | `PRECISE_CODE_INTEL_COMMIT_RESOLVER_BATCH_SIZE` | `100` | The maximum number of unique commits to resolve at a time. | | `PRECISE_CODE_INTEL_COMMIT_RESOLVER_MAXIMUM_COMMIT_LAG` | `0s` | The maximum acceptable delay between accepting an upload and its commit becoming resolvable. Be cautious about setting this to a large value, as uploads for unresolvable commits will be retried periodically during this interval. | | `PRECISE_CODE_INTEL_RETENTION_REPOSITORY_PROCESS_DELAY` | `24h` | The minimum frequency that the same repository's uploads can be considered for expiration. | | `PRECISE_CODE_INTEL_RETENTION_REPOSITORY_BATCH_SIZE` | `100` | The number of repositories to consider for expiration at a time. | | `PRECISE_CODE_INTEL_RETENTION_UPLOAD_PROCESS_DELAY` | `24h` | The minimum frequency that the same upload record can be considered for expiration. | | `PRECISE_CODE_INTEL_RETENTION_UPLOAD_BATCH_SIZE` | `100` | The number of uploads to consider for expiration at a time. | | `PRECISE_CODE_INTEL_RETENTION_POLICY_BATCH_SIZE` | `100` | The number of policies to consider for expiration at a time. | | `PRECISE_CODE_INTEL_RETENTION_COMMIT_BATCH_SIZE` | `100` | The number of commits to process per upload at a time. | | `PRECISE_CODE_INTEL_RETENTION_BRANCHES_CACHE_MAX_KEYS` | `10000` | The number of maximum keys used to cache the set of branches visible from a commit. | | `PRECISE_CODE_INTEL_CONFIGURATION_POLICY_MEMBERSHIP_BATCH_SIZE` | `100` | The maximum number of policy configurations to update repository membership for at a time. | | `PRECISE_CODE_INTEL_DOCUMENTATION_SEARCH_CURRENT_MINIMUM_TIME_SINCE_LAST_CHECK` | `24h` | The minimum time the documentation search current janitor will re-check records for a unique search key. | | `PRECISE_CODE_INTEL_DOCUMENTATION_SEARCH_CURRENT_BATCH_SIZE` | `100` | The maximum number of unique search keys to clean up at a time. | ## precise-code-intel-worker The following are variables are read from the `precise-code-intel-worker` service to control code graph data upload processing behavior. | **Name** | **Default** | **Description** | | ----------------------- | ------- | ------------------------------------------------------------------------------------------------------------------ | | `PRECISE_CODE_INTEL_WORKER_POLL_INTERVAL` | `1s` | Interval between queries to the upload queue. | | `PRECISE_CODE_INTEL_WORKER_CONCURRENCY` | `1` | The maximum number of indexes that can be processed concurrently. | | `PRECISE_CODE_INTEL_WORKER_BUDGET` | `0` | The amount of compressed input data (in bytes) a worker can process concurrently. Zero acts as an infinite budget. | The following settings should be the same for the [`frontend`](#frontend) service as well. | **Name** | **Default** | **Description** | | ----------------------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `PRECISE_CODE_INTEL_UPLOAD_BACKEND` | `Blobstore` | The target file service for code graph data uploads. S3, GCS, and Blobstore are supported. In older versions of Sourcegraph (before v3.4.2) `Minio` was also a valid value. | | `PRECISE_CODE_INTEL_UPLOAD_MANAGE_BUCKET` | `false` | Whether or not the client should manage the target bucket configuration | | `PRECISE_CODE_INTEL_UPLOAD_BUCKET` | `lsif-uploads` | The name of the bucket to store LSIF uploads in | | `PRECISE_CODE_INTEL_UPLOAD_TTL` | `168h` | The maximum age of an upload before deletion |
# Auto-indexing configuration reference This feature is in beta for self-hosted customers. This document details the expected contents of [explicit index job configuration](/code-search/code-navigation/auto_indexing#explicit-index-job-configuration) for a particular repository. Depending on how this configuration is supplied, it may be encoded as JSON. ## Keys The root of the configuration has one top-level key, `index_jobs`, documented below. ### [`index_jobs`](#index-jobs) The index jobs field defines a set of [index job objects](#index-job-object) describing the actions to perform to successfully index a fresh clone of a repository. Each index job is executed independently (and possibly in parallel) by an executor. ## Index job object Each configured index job is run by a single executor process. Multiple index jobs configured for the same repository may be executed by different executor instances, out of order, and possibly in parallel. The basic outline of an index job is as follows. 1. The target repository is cloned into the job's workspace from Sourcegraph. 2. Pre-indexing steps (if any are configured) are executed sequentially in individual Docker containers that volume mount the workspace. 3. A separate Docker container for the indexer is started. 4. Local steps (if configured) are executed within the running container. 5. The indexer is invoked within the running container to produce an index artifact. 6. The [`src` CLI](/cli/) binary is invoked to upload the index artifact to the Sourcegraph instance. The pre-indexing steps, indexer container, local steps, and indexer arguments are configurable via this object. ### Keys Each indexing job object can be configured with the following keys. #### [`steps`](#index-job-steps) The steps field defines an ordered sequence of pre-indexing actions (formatted as a [Docker step object](#docker-step-object)). Each step is executed before the indexer itself is invoked. #### [`indexer`](#index-job-indexer) The name of the Docker image distribution of the target indexer. #### [`local_steps`](#index-job-local-steps) An ordered sequence of commands to execute within a container running the configured indexer image. These commands are passed directly into the target container via `docker exec`, one at a time. Local steps should be used over Docker steps when the intended side effects alter state outside of the workspace on disk (e.g., setting up environment variables or installing OS packages require by the indexer tool). #### [`indexer_args`](#index-job-indexer-args) An ordered sequence of arguments that make up the indexing command. The indexing command is passed directly into the target container via `docker exec`. This step is expected to produce a code graph index artifact (as described by the `root` and `outfile` fields, described below). #### [`root`](#index-job-root) The working directory within the Docker container where the provided local steps and indexer commands are executed. This working directory is relative to the root of the target repository. An empty value (the default) indicates the root of the repository. This is also the directory relative to the path where the code graph index artifact is produced. #### [`outfile`](#index-job-outfile) The path to the code graph index artifact produced by the indexer, which is uploaded to the target Sourcegraph instance via [`src` CLI](/cli/) after the index step has completed successfully. This path is relative to the index job `root` (defined above). If not supplied, the value is assumed to be `dump.lsif` (which is the default artifact name of many indexers). Supply this argument when the target indexer produces a differently named artifact. Alternatively, some indexers provide flags to change the artifact name; in which case `dump.lsif` can be supplied there and a value for this key can be omitted. #### [`requested_envvars`](#index-job-requested-envvars) List of environment variables needed. These are made accessible to steps, local_steps, and the indexer_args command. ### Examples The following example uses the Docker image `sourcegraph/scip-go` pinned at the tag `v0.1` and additionally secured with an image digest. This index configuration runs the Go indexer with quiet output in the `dev/sg` directory and uploads the resulting index file (`index.scip` by default). ```json { "indexer": "sourcegraph/scip-go:v0.1@sha256::39c1495...55391cc", "indexer_args": [ "scip-go", "-q", ], "root": "dev/sg" } ``` The following example uses the Docker image `sourcegraph/scip-typescript` pinned at the tag `autoindex`. This index configuration will run `npm install` followed by the TypeScript indexer `scip-typescript` in the `editors/code` directory. In this job, both commands run in the same workspace, but are invoked in different Docker containers. Both containers happen to be based on the same image, but that's not necessary. Note: `sourcegraph/scip-typescript` does not define a Docker entrypoint, so we need to indicate which binary to invoke (along with any additional arguments). ```json { "steps": [ { "image": "sourcegraph/scip-typescript:autoindex", "commands": [ "npm install" ], "root": "editors/code" } ], "indexer": "sourcegraph/scip-typescript:autoiondex", "indexer_args": [ "scip-typescript", "index" ], "root": "editors/code" } ``` ## Docker step object Each configured Docker step is executed sequentially using the same volume-mounted workspace, which is initially seeded with a fresh clone of the target repository. If the contents of the workspace is modified by one step, the changes will be visible in the next. This makes Docker steps especially useful for pre-indexing tasks that require modification of the source code, such as dependency resolution (e.g., `npm install`, `go mod download`) and code generation (e.g., `go generate ./...`) required for successful compilation or indexing of the project. ### Keys Each Docker step object can be configured with the following keys. #### [`image`](#docker-step-image) The name of the Docker image in which the configured commands are executed. #### [`commands`](#docker-step-commands) An ordered sequence of commands to execute within a container running the configured image. These commands are passed directly into the target container via `docker exec`, one at a time. #### [`root`](#docker-step-root) The working directory within the Docker container where the provided commands are executed. This working directory is relative to the root of the target repository. An empty value (the default) indicates the root of the repository. ### Examples The following example runs `go generate` over all packages in the root of the repository. ```json { "indexer": "golang:1.17.3-buster@sha256:c1bae5f...3e4f07b", "commands": [ "go generate ./..." ] } ``` The following example runs a series of commands to install dependencies in the `lib/proj` directory. ```json { "indexer": "node:alpine3.12@sha256:4435145...fd06fd3", "commands": [ "yarn config set ignore-engines true", "yarn install" ], "root": "lib/proj" } ``` # Auto-indexing Supported on [Enterprise](/pricing/enterprise) plans. Currently in Beta and available via Web app.

Learn and understand how auto-indexing works.

With Sourcegraph deployments supporting [executors](/admin/executors/), your repository contents can be automatically analyzed to produce a code graph index file. Once [auto-indexing is enabled](/code-search/code-navigation/auto_indexing#enable-auto-indexing) and [auto-indexing policies are configured](/code-search/code-navigation/auto_indexing#configure-auto-indexing), repositories will be periodically cloned into an executor sandbox, analyzed, and the resulting index file will be uploaded back to the Sourcegraph instance. Auto-indexing is currently available for Go, TypeScript, JavaScript, Python, Ruby and JVM repositories. See also [dependency navigation](/code-search/code-navigation/features#dependency-navigation) for instructions on how to setup cross-dependency navigation depending on what language ecosystem you use. ## Enable auto-indexing The following docs explains how to turn on [auto-indexing](/code-search/code-navigation/auto_indexing) on your Sourcegraph instance to enable [precise code navigation](/code-search/code-navigation/precise_code_navigation). ### Deploy executors This step is only required if you are on self-hosted Sourcegraph. First, [deploy the executor service](/admin/executors/deploy_executors) targeting your Sourcegraph instance. This will provide the necessary compute resources that clone the target Git repository, securely analyze the code to produce a code graph data index, then upload that index to your Sourcegraph instance for processing. ### Enable index job scheduling Next, enable the following feature flag in your Sourcegraph instance's site configuration. ```yaml { "codeIntelAutoIndexing.enabled": true } ``` This step will control the scheduling of indexing jobs which are made available to the executors deployed in the previous step. ### Tune the index scheduler The frequency of index job scheduling can be tuned via the following environment variables read by `worker` service containers running the [`codeintel-auto-indexing`](/admin/workers#codeintel-auto-indexing) task. - **`PRECISE_CODE_INTEL_AUTO_INDEXING_TASK_INTERVAL`**: The time to run periodic codeintel auto-indexing tasks. The default is every 2 minutes - **`PRECISE_CODE_INTEL_AUTO_INDEXING_REPOSITORY_PROCESS_DELAY`**: The minimum time that the same repository can be considered for auto-index scheduling. The default is every 24 hours - **`PRECISE_CODE_INTEL_AUTO_INDEXING_REPOSITORY_BATCH_SIZE`**: The number of repositories to consider for auto-indexing scheduling at a time. Default is 2500 - **`PRECISE_CODE_INTEL_AUTO_INDEX_MAXIMUM_REPOSITORIES_INSPECTED_PER_SECOND`**: The maximum number of repositories inspected for auto-indexing per second. Set to zero to disable the limit. Default is 0 ## Configure auto-indexing This feature is in beta for self-hosted customers. Precise code navigation [auto-indexing](/code-search/code-navigation/auto_indexing) jobs are scheduled based on two fronts of configuration. The first front selects the set of repositories and commits within those repositories that are candidates for auto-indexing. These candidates are controlled by [configuring auto-indexing policies](#configure-auto-indexing-policies). The second front determines the set of index jobs that can run over candidate commits. By default, index jobs are [inferred](/code-search/code-navigation/inference_configuration) from the repository structure's on disk. Index job inference uses heuristics such as the presence or contents of particular files to determine the paths and commands required to index a repository. Alternatively, index job configuration [can be supplied explicitly](#explicit-index-job-configuration) for a repository when the inference heuristics are not powerful enough to create an index job that produces the correct results. This might be necessary for projects that have non-standard or complex dependency resolution or pre-compilation steps, for example. ### Configure auto-indexing policies Let's learn how to configure policies to control the scheduling of precise code navigation indexing jobs. An indexing jobs [produces a code graph data index](/code-search/code-navigation/precise_code_navigation) and uploads it to your Sourcegraph instance for use with code navigation. See the [best practices guide](/code-search/code-navigation/how-to/policies_resource_usage_best_practices) for additional details on how policies affect resource usage. Each policy has a number of configurable options, including: - The set of Git branches or tags to which the policy applies - The maximum age of commits that should be indexed (e.g., skip indexing commits made last year) - For branches, whether to only consider the _tip_ of the branch, or all commits on that branch When auto-indexing is enabled, uploading a code graph data index will trigger creation of index jobs for dependencies if they are available on the Sourcegraph instance, in order to provide greater coverage for precise code navigation actions such as Go to definition and Find references. Precise code navigation indexing jobs are scheduled periodically in the background for each repository matching an indexing policy. ### Applying indexing policies globally Site admins can create indexing policies that apply to **all repositories** on their Sourcegraph instance. In order to view and edit these policies, navigate to the code graph configuration in the site-admin dashboard. ![Global auto-indexing policy configuration list page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/global-list-indexing.png) New policies can also be created to apply to the HEAD of the default branch, or to apply to any arbitrary Git branch or tag pattern. For example, you may want to index release branches for all of your repositories (in this example, branches whose last commit is older than five years of age will not apply). ![Global auto-indexing policy configuration edit page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/global-create-indexing.png) ![lobal auto-indexing policy configuration created confirmation](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/post-create.png) New policies can be created to apply to a set of repositories that are matched by name. For example, you may want to enable indexing for a particular set of repositories (in this example, repositories in the `sourcegraph` organization). ![Global auto-indexing policy with repository patterns configuration edit page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/create-repo-list-indexing.png) ![Global auto-indexing policy with repository patterns configuration created confirmation](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/post-create-repo-list.png) ### Applying indexing policies to a specific repository Indexing policies can also be created on a per-repository basis as commit and merge workflows differ wildly from project to project. In order to view and edit repository-specific policies, navigate to the code graph settings in the target repository's index page. ![Repository index page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/sg-3.33/repository-page.png) The settings page will show all policies that apply to the given repository, including both repository-specific policies as well as global policies that match the repository. ![Repository-specific auto-indexing policy configuration list page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/list.png) In this example, we create an indexing policy that applies to all _versioned_ tags (those prefixed with `v`). The **Index all version tags** policy ensures all commits visible from matching tagged commit will be kept indexed (and not removed due to age). ![Repository-specific auto-indexing policy configuration edit page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/create.png) ![Repository-specific auto-indexing policy configuration created confirmation](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/post-create-indexing.png) ### Explicit index job configuration For projects that have non-standard or complex dependency resolution or pre-compilation steps, inference heuristics might not be powerful enough to create an index job that produces the correct results. In these cases, explicit index job configuration can be supplied to a repository in two ways (listed below in order of decreasing precedence). Both methods of configuration share a common expected schema. See the [reference documentation](/code-search/code-navigation/auto_indexing_configuration) for additional information on the shape and content of the configuration. 1. Configure index jobs by committing a `sourcegraph.yaml` file to the root of the target repository. If you're new to YAML and want a short introduction, see [Learn YAML in five minutes](https://learnxinyminutes.com/docs/yaml/). Note that YAML is a strict superset of JSON, therefore the file contents can also be encoded as valid JSON (despite the file extension). 2. Configure index jobs via the target repository's code graph settings UI. In order to view and edit the indexing configuration for a repository, navigate to the code graph settings in the target repository's index page. ![Repository index page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/sg-3.33/repository-page.png) From there you can view or edit the repository's configuration. We use a superset of JSON that allows for comments and trailing commas. The set of index jobs that would be [inferred](/code-search/code-navigation/explanations/auto_indexing_inference) from the content of the repository (at the current tip of the default branch) can be viewed and may often be useful as a starting point to define more elaborate indexing jobs. ![Auto-indexing configuration editor](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/configuration.png) ### Private repositories and packages configuration For auto-indexing jobs to be able to build your projects that use private repositories and packages, you need to provide language-specific configuration in the form of Executor secrets. ### Go For Go resolver to access private Git repositories, you need to configure a `NETRC_DATA` secret with the following contents: ```text machine login password ``` Under the hood, this information will be used to write the [.netrc](https://www.gnu.org/software/inetutils/manual/html_node/The-_002enetrc-file.html) file that is respected by Git and Go. ### TypeScript/JavaScript For **NPM**, you can create a [secret named `NPM_TOKEN`](https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow#set-the-token-as-an-environment-variable-on-the-cicd-server) which will be automatically picked up by the indexer. ## Language support Auto-indexing is currently available for Go, TypeScript, JavaScript, Python, Ruby and JVM repositories. See also [dependency navigation](/code-search/code-navigation/features#dependency-navigation) for instructions on how to setup cross-dependency navigation depending on what language ecosystem you use. ## Lifecycle of an indexing job Index jobs are run asynchronously from a queue. Each index job has an attached **state** that can change over time as work associated with that job is performed. The following diagram shows transition paths from one possible state of an index job to another. ![Index job state diagram](https://storage.googleapis.com/sourcegraph-assets/Docs/index-states.svg) The general happy-path for an index job is: `QUEUED_FOR_INDEXING`, `INDEXING`, then `INDEXING_COMPLETED`. Once uploaded, the remaining lifecycle is described in [lifecycle of an upload](/code-search/code-navigation/explanations/uploads#lifecycle-of-an-upload). Index jobs may fail to complete due to the job configuration not aligning with the repository contents or due to transient errors related to the network (for example). An index job will enter the `INDEXING_ERRORED` state on such conditions. Errored index jobs may be retried a number of times before moving into a permanently errored state. At any point, an index job record may be deleted (usually due to explicit deletion by the user). ## Lifecycle of an indexing job (via UI) Users can see precise code navigation index jobs for a particular, repository by navigating to the code graph page in the target repository's index page. ![Repository index page](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/sg-3.33/repository-page.png) Administrators of a Sourcegraph instance can see a global view of code graph index jobs across all repositories from the **Site Admin** page. ![Global list of precise code navigation index jobs across all repositories](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/indexes-list.png) The detail page of an index job will show its current state as well as detailed logs about its execution up to the current point in time. ![Upload in processing state](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/indexes-processing.png) The stdout and stderr of each command run during pre-indexing and indexing steps are viewable as the index job is processed. This information is valuable when troubleshooting a [custom index configuration](/code-search/code-navigation/auto_indexing_configuration) for your repository. ![Detailed look at index job logs](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/indexes-processing-detail.png) Once the index job completes, a code graph data file has been uploaded to the Sourcegraph instance. ![Upload in completed state](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/renamed/indexes-completed.png) ## Inference of auto-indexing jobs This feature is in beta for self-hosted customers. When a commit of a repository is selected as a candidate for [auto-indexing](/code-search/code-navigation/auto_indexing) but does not have an explicitly supplied index job configuration, index jobs are inferred from the content of the repository at that commit. The site-config setting `codeIntelAutoIndexing.indexerMap` can be used to update the indexer image that is (globally) used on inferred jobs. For example, `"codeIntelAutoIndexing.indexerMap": {"go": "lsif-go:alternative-tag"}` will cause inferred jobs indexing Go code to use the specified container (with an alternative tag). This can also be useful for specifying alternative Docker registries. This document describes the heuristics used to determine the set of index jobs to schedule. See [configuration reference](/code-search/code-navigation/auto_indexing_configuration) for additional documentation on how index jobs are configured. As a general rule of thumb, an indexer can be invoked successfully if the source code to index can be compiled successfully. The heuristics below attempt to cover the common cases of dependency resolution, but may not be sufficient if the target code requires additional steps such as code generation, header file linking, or installation of system dependencies to compile from a fresh clone of the repository. For such cases, we recommend using the inferred job as a starting point to [explicitly supply index job configuration](/code-search/code-navigation/auto_indexing#explicit-index-job-configuration). ### Go For each directory containing a `go.mod` file, the following index job is scheduled. ```json { "indexing_jobs": [ { "steps": [ { "root": "", "image": "sourcegraph/lsif-go", "commands": [ "go mod download" ] } ], "root": "", "indexer": "sourcegraph/lsif-go", "indexer_args": [ "lsif-go", "--no-animation" ] } ] } ``` For every other directory excluding `vendor/` directories and their children containing one or more `*.go` files, the following index job is scheduled. ```json { "root": "", "indexer": "sourcegraph/lsif-go", "indexer_args": [ "GO111MODULE=off", "lsif-go", "--no-animation" ] } ``` ### TypeScript For each directory excluding `node_modules/` directories and their children containing a `tsconfig.json` file, the following index job is scheduled. Note that there are a dynamic number of pre-indexing steps used to resolve dependencies: for each ancestor directory `ancestor(dir)` containing a `package.json` file, the dependencies are installed via either `yarn` or `npm`. These steps run in order, depth-first. ```json { "steps": [ { "root": "", "image": "sourcegraph/scip-typescript:autoindex", "commands": [ "yarn" ] }, { "root": "", "image": "sourcegraph/scip-typescript:autoindex", "commands": [ "npm install" ] }, "..." ], "local_steps": [ "N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release n --arch x64-musl autol" ], "root": "", "indexer": "sourcegraph/scip-typescript:autoindex", "indexer_args": [ "scip-typescript", "index" ] } ``` ### Rust If the repository contains a `Cargo.toml` file, the following index job is scheduled. ```json { "root": "", "indexer": "sourcegraph/lsif-rust", "indexer_args": [ "lsif-rust", "index" ], "outfile": "dump.lsif" } ``` ### Java Inference for languages supported by [scip-java](https://github.com/sourcegraph/scip-java) is currently restricted to Sourcegraph.com. If the repository contains both a `lsif-java.json` file as well as `*.java`, `*.scala`, or `*.kt` files, the following index job is scheduled. ```json { "root": "", "indexer": "sourcegraph/scip-java", "indexer_args": [ "scip-java", "index", "--build-tool=lsif" ], "outfile": "index.scip" } ``` ## More resources # Code intelligence policies resource usage best practices This guide gives an overview of best practices when defining [auto-index scheduling](/code-search/code-navigation/auto_indexing#configure-auto-indexing) and data retention policies as it relates to resource usage (particular the disk requirement for Postgres). **Auto-index scheduling policies** define the _cadence_ at which particular repositories will be subject to have indexing jobs scheduled (depending on the repository's configuration and contents). These policies define when a commit of a repository should be marked as an auto-index job candidate. **Data retention policies** define the set of indexes which are _interesting_ to a particular Sourcegraph instance's users. This is generally not *all* commits (or even all repositories), and can change drastically from company-to-company. SCIP indexes for the head of a repository's default branch are useful to explore what is most likely the current view of the development branch. These are safe from expiration by default and do not require an explicit policy to be defined by the user. SCIP indexes for historic commits can be useful to retain in addition to the current development target (retained implicitly). SCIP indexes associated with git tags matching `v*` (for example) may be useful to index and retain indefinitely (or for some relevant length of time). These indexes are _more_ likely to be used by other projects, running in production, or used externally than any arbitrary commit of the repository. Branches with a particular format may also have significance indicating its indexes should not be garbage collected. Policies should be created in such a manner that the commits most useful to _your_ engineers are covered. This may mean defining both an auto-indexing and data retention policy, or ensuring that SCIP indexes uploaded externally (via build system) are covered by a data retention policy. However, _usefulness_ vs _cost_ can be a delicate balancing act. If all SCIP indexes uploaded to an instance were retained forever, then disk requirements for Postgres would grow continuously. This is not sustainable for any engineering team with a non-zero commit velocity. If your engineering team has zero velocity please contact Sourcegraph support for guidance. Both of these types of policies (as well as any SCIP uploads integrated into a build system) influence the usage of disk in the `codeintel-db` schema. There are two large knobs that can be turned to reduce usage: 1. Reduce index coverage (exclude repositories or targets or branches) or index less frequently 2. Retain fewer indexes as they age (e.g., do not keep indexes on the non-head of branches) The following factors also play a contributing role to the scale of data on a particular instance: - How *large* your repositories (or indexing targets) are - How *frequently* your repositories (or indexing targets) receive new commits - How *widely* your data retention policies retain individual SCIP indexes (one branch vs all branches) - How *deeply* your data retention policies retain individual SCIP indexes (the latest commit vs all commits on a branch) - How many repositories (or indexing targets) have received SCIP uploads (are there deprecated repos being indexed that will never be relevant to a user?) ## Sourcegraph.com's configuration As an example, we detail relevant configuration for [Sourcegraph.com](https://sourcegraph.com). - We support auto-indexing on repositories matching `github.com/*` and `gitlab.com/*`, but only index the `HEAD` of these repositories as there are a massive number and indexing branches or tags of both code hosts would be infeasible. Matching indexes are retained indefinitely, but there is only one set of indexes covered by this policy per repository at any given time. This policy doesn't keep unnecessary data longer than necessary. - We support indexing of all tags of `jdk` with infinite retention to support precise navigation into Java ecosystem internals when individual repositories have precise intelligence. - Experimentally, we're indexing all tags of repositories matching `maven/*` to get quantitative measurements on the number of open-source Java projects we're able to infer build/indexing requirements from given only the repository contents. # Index other languages

This guide is meant to provide instructions for how to enable precise code navigation for any programming language.

The general steps for enabling precise code navigation are as follows: ## Install Sourcegraph CLI The [Sourcegraph CLI](/cli/) is used for uploading SCIP data to your Sourcegraph instance (replace `linux` with `darwin` for macOS): ```sh curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src ``` ## Install SCIP indexer An SCIP indexer is a command line tool that performs code analysis of source code and outputs a file with metadata containing all the definitions, references, and hover documentation in a project in SCIP. The SCIP file is then uploaded to Sourcegraph to power code navigation features. Install the [indexer](/code-search/code-navigation/writing_an_indexer) for the required programming language of your repository by following the instructions in the indexer's README ## Generate SCIP data To generate the SCIP data for your repository run the command in the _generate SCIP data_ step found in the README of the installed indexer. ## Upload SCIP data The upload step is the same for all languages. Make sure the current working directory is a path inside your repository, then use the Sourcegraph CLI to upload the SCIP file: ### To a private Sourcegraph instance (on prem) ```sh SRC_ENDPOINT=https://sourcegraph.mycompany.com src code-intel upload -file=index.scip ``` ### To cloud based Sourcegraph.com ```sh src code-intel upload -github-token=YourGitHubToken -file=dump.lsif ``` The `src-cli` upload command will try to infer the repository and git commit by invoking git commands on your local clone. If git is not installed, is older than version 2.7.0 or you are running on code outside of a git clone, you will need to also specify the `-repo` and `-commit` flags explicitly. If you're using Sourcegraph.com or have enabled [`lsifEnforceAuth`](/admin/config/site_config#lsifEnforceAuth) you need to [supply a GitHub token](#proving-ownership-of-a-github-repository) supplied via the `-github-token` flag in the command above. Successful output will appear similar to the following example. ![Uploading a code graph index via the Sourcegraph CLI](https://storage.googleapis.com/sourcegraph-assets/docs/images/code-intelligence/sg-3.34/uploads/src-lsif-upload.gif) ## Automate code indexing Now that you have successfully enabled code navigation for your repository, you can automate source code indexing to ensure precise code navigation stays up to date with the most recent code changes in the repository. See our [continuous integration guide](/code-search/code-navigation/how-to/adding_lsif_to_workflows) to setup automation. ## Troubleshooting ### Testing Make sure you have configured your Sourcegraph instance and [enabled precise code navigation](/code-search/code-navigation/precise_code_navigation#setting-up-code-navigation-for-your-codebase). Once LSIF data has uploaded, open the Sourcegraph UI or your code host (i.e. GitHub) and navigate to any code file that was part of the repository that was analyzed by the LSIF indexer. Hover over a symbol, variable or function name in the file, you should now see rich LSIF metadata as the source for hover-tooltips, definitions, and references. ### Error Logs To view code graph indexer processing failures go to **Repository settings > Code graph data > Activity for this repository** in your Sourcegraph instance. Failures can occur if the LSIF data is invalid (e.g., malformed indexer output), or problems were encountered during processing (e.g., system-level bug, flaky connections, etc). If you are unable to resolve the issue, please [contact support](https://help.sourcegraph.com/hc/en-us/requests/new). ### Common Errors Possible errors that can happen during upload include: - Clone in progress: the instance doesn't have the necessary data to process your upload yet, retry in a few minutes - Unknown repository (404): check your `-endpoint` and make sure you can view the repository on your Sourcegraph instance - Invalid commit (404): try visiting the repository at that commit on your Sourcegraph instance to trigger an update - Invalid auth when using Sourcegraph.com or when [`lsifEnforceAuth`](/admin/config/site_config#lsifEnforceAuth) is `true` (401 for an invalid token or 404 if the repository cannot be found on GitHub.com): make sure your GitHub token is valid and that the repository is correct - Unexpected errors (500s): reach out to your Sourcegraph support team for assistance
# Index a TypeScript or JavaScript repository

This page describes how to create an index for JavaScript and TypeScript projects and uploading it to Sourcegraph.

We will use [`scip-typescript`](https://github.com/sourcegraph/scip-typescript) to create the index and the [Sourcegraph CLI](https://github.com/sourcegraph/src-cli) to upload it to Sourcegraph. ## Indexing in CI using scip-typescript directly In this approach, you will directly install `scip-typescript` and `src-cli` in CI. This is particularly useful if you are already using some other Docker image for your build. Here is an example using GitHub Actions to create and upload an index for a TypeScript project. ```yaml jobs: create-index-and-upload: # prevent forks of this repo from uploading lsif indexes if: github.repository == '' runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Install dependencies run: npm install - name: Install scip-typescript run: npm install -g @sourcegraph/scip-typescript - name: Generate index uses: scip-typescript index - name: Install src-cli run: | curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src - name: Upload index run: src code-intel upload -github-token='${{ secrets.GITHUB_TOKEN }}' -no-progress env: SRC_ENDPOINT: https://sourcegraph.com/ ``` `src-cli` ignores index upload failures by default to avoid disrupting CI pipelines with non-critical errors. On CI providers other than GitHub Actions, you may need to explicitly install [Node.js](https://nodejs.org/) as a first step. See the [`scip-typescript` README](https://github.com/sourcegraph/scip-typescript) for the list of supported Node.js versions. Examples: - [lodash/lodash](https://github.com/sourcegraph-codeintel-showcase/lodash/blob/master/.github/workflows/lsif.yml) (JavaScript) ### Optional scip-typescript flags The exact `scip-typescript` invocation will vary based on your configuration. For example: - If you are indexing a JavaScript project instead of TypeScript, add the `--infer-tsconfig` flag. ```sh scip-typescript index --infer-tsconfig ``` - If you are indexing a project using Yarn workspaces, add the `--yarn-workspaces` flag. ```sh scip-typescript index --yarn-workspaces ``` - If you are indexing a project using Pnpm workspaces, add the `--pnpm-workspaces` flag. ```sh scip-typescript index --pnpm-workspaces ``` ## Indexing in CI using the scip-typescript Docker image Sourcegraph provides a Docker image for `sourcegraph/scip-typescript`, which bundles `src-cli` for convenience. Here is an example using the `scip-typescript` Docker image with GitHub Actions to index a TypeScript project. ```yaml jobs: create-and-upload-index: # prevent forks of this repo from uploading lsif indexes if: github.repository == '' runs-on: ubuntu-latest container: sourcegraph/scip-typescript:latest steps: - uses: actions/checkout@v3 - name: Install dependencies run: npm install - name: Generate index run: scip-typescript index - name: Upload index run: src code-intel upload -github-token=${{ secrets.GITHUB_TOKEN }} -no-progress env: SRC_ENDPOINT: https://sourcegraph.com/ ``` If you are indexing a JavaScript codebase or a project using Yarn workspaces, tweak the `scip-typescript` invocation as documented in the [Optional scip-typescript flags](#optional-scip-typescript-flags) section. ## One-off indexing using scip-typescript locally Creating one-off indexes and uploading them is valuable as a proof of concept, but indexes are not kept up to date. The steps here are similar to those in the previous GitHub Actions example. 1. Install `scip-typescript`. ```sh npm install -g @sourcegraph/scip-typescript ``` 2. Install the Sourcegraph CLI. ```sh curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src ``` The exact invocation may change depending on the OS and architecture. See the [`src-cli` README](https://github.com/sourcegraph/src-cli#installation) for details. 3. `cd` into your project's root (which contains `package.json`/`tsconfig.json`) and run the following: ```sh npm install # Enable (1) type-checking code used from external packages and (2) cross-repo navigation by installing dependencies first with npm or yarn scip-typescript index # for TypeScript projects ``` If you are indexing a JavaScript codebase or a project using Yarn workspaces, tweak the `scip-typescript` invocation as documented in the [Optional scip-typescript flags](#optional-scip-typescript-flags) section. 4. Upload the data to a Sourcegraph instance. ```sh SRC_ENDPOINT= src code-intel upload # for private instances src code-intel upload -github-token= # for public instances ``` The upload command will provide a URL you can visit to see the upload status. Once the upload has finished processing, you can visit the repo and enjoy precise code navigation!
# Go SCIP indexing

This page describes how you can automate data indexing in SCIP for Go codebases or index data manually.

## Automated indexing Sourcegraph provides the Docker images `sourcegraph/scip-go` and `sourcegraph/src-cli` so that you can easily automate indexing in your favorite CI framework. Note that the `scip-go` image bundles `src-cli`, so the second image may not be necessary. The following examples show you how to set up automated indexing in a few popular frameworks. You'll need to substitute the indexer and upload commands with what works for your project locally. If you implement automated indexing in a different framework, feel free to edit this page with instructions! ### GitHub Actions ```yaml on: - push jobs: scip-go: # this line will prevent forks of this repo from uploading lsif indexes if: github.repository == '' runs-on: ubuntu-latest container: sourcegraph/scip-go:latest steps: - uses: actions/checkout@v1 - name: Generate SCIP data run: scip-go - name: Install src CLI run: | curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src - name: Upload SCIP data # this will upload to Sourcegraph.com, you may need to substitute a different command. # by default, we ignore failures to avoid disrupting CI pipelines with non-critical errors. run: src code-intel upload -github-token=${{ secrets.GITHUB_TOKEN }} -ignore-upload-failure ``` The following projects have example GitHub Actions workflows to generate and upload LSIF indexes: - [golang/go](https://github.com/sourcegraph-codeintel-showcase/go/blob/f40606b1241b0ca4802d7b00a763241b03404eea/.github/workflows/lsif.yml) - [kubernetes/kubernetes](https://github.com/sourcegraph-codeintel-showcase/kubernetes/blob/master/.github/workflows/lsif.yml) - [moby/moby](https://github.com/sourcegraph-codeintel-showcase/moby/blob/master/.github/workflows/lsif.yml) ### CircleCI ```yaml version: 2.1 jobs: scip-go: docker: - image: sourcegraph/scip-go:latest steps: - checkout - run: scip-go - run: | curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src; # this will upload to Sourcegraph.com, you may need to substitute a different command. # by default, we ignore failures to avoid disrupting CI pipelines with non-critical errors. - run: src code-intel upload -github-token=<> -ignore-upload-failure workflows: scip-typescript: jobs: - scip-typescript ``` The following projects have example CircleCI configurations to generate and upload SCIP indexes. - [grafana](https://github.com/sourcegraph-codeintel-showcase/grafana/blob/master/.circleci/config.yml) - [helm](https://github.com/sourcegraph-codeintel-showcase/helm/blob/62c38f152d0802719aad1ec4c1c281f01dc75173/.circleci/config.yml) - [prometheus/prometheus](https://github.com/sourcegraph-codeintel-showcase/prometheus/blob/a0a8a249fff9d1c6ce4c097ccc4f5e120c723c51/.circleci/config.yml) ### Travis CI ```yaml services: - docker jobs: include: - stage: scip-go # this will upload to Sourcegraph.com, you may need to substitute a different command. # by default, we ignore failures to avoid disrupting CI pipelines with non-critical errors. script: - | docker run --rm -v $(pwd):/src -w /src sourcegraph/scip-go:latest /bin/sh -c \ "scip-go; curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src; chmod +x /usr/local/bin/src; src code-intel upload -github-token=$GITHUB_TOKEN -ignore-upload-failure" ``` The following projects have example Travis CI configurations to generate and upload SCIP indexes. - [aws/aws-sdk-go](https://github.com/sourcegraph-codeintel-showcase/aws-sdk-go/blob/92f67a061fcdd46d6a418b28838b10b6ac63a880/.travis.yml) - [etcd-io/etcd](https://github.com/sourcegraph-codeintel-showcase/etcd/blob/eae726706fe8ebf7e08b45ba29a70388595db31b/.travis.yml) - [hugo/hugo](https://github.com/sourcegraph-codeintel-showcase/hugo/blob/6704b7c125d7b21ccf2048d7bff0f1ffe2b0867d/.travis.yml) ## Manual indexing 1. Install the [Go SCIP indexer](https://github.com/sourcegraph/scip-go). 1. Install the [Sourcegraph CLI](https://github.com/sourcegraph/src-cli) with the following command: ```sh curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src ``` - **macOS**: Replace `linux` with `darwin` in the URL and choose the appropriate architecture: M1/M2 chips - `arm64`, Intel chips - `amd64`. - **Windows**: Visit [the CLI repo](https://github.com/sourcegraph/src-cli) for further instructions. 1. `cd` into your Go project's root (where the `go.mod` file lives, if you have one) and run: ```sh scip-go # generates a file named index.scip ``` 1. Upload the data to a Sourcegraph instance with: ``` sh src -endpoint= code-intel upload # for private instances src code-intel upload -github-token= # for public instances ``` The upload command will provide a URL you can visit to see the upload status. When the upload is complete, you can visit the repo and check out the difference in code navigation quality!
# How-to guides - [Adding precise code navigation to CI/CD workflows](/code-search/code-navigation/how-to/adding_lsif_to_workflows) - [Combining SCIP uploads from CI/CD and auto-indexing](/code-search/code-navigation/how-to/combining_scip_uploads_from_ci_cd_and_auto_indexing) - [Go SCIP indexing](/code-search/code-navigation/how-to/index_a_go_repository) - [Index a TypeScript or JavaScript repository](/code-search/code-navigation/how-to/index_a_typescript_and_javascript_repository) - [Index other languages](/code-search/code-navigation/how-to/index_other_languages) - [Code intelligence policies resource usage best practices](/code-search/code-navigation/how-to/policies_resource_usage_best_practices) # Combining SCIP uploads from CI/CD and auto-indexing Sourcegraph Enterprise instances can serve many profiles of repository size and build complexity, and therefore provides multiple methods precise SCIP index data for your team's repositories. We currently support: 1. Uploading [SCIP data yourself](/code-search/code-navigation/how-to/index_other_languages#upload-scip-data) directly from an already-configured build or continuous integration server, as well as 2. [Auto-indexing](/code-search/code-navigation/auto_indexing), which schedules index creation within the Sourcegraph instance. There is nothing preventing users from mix-and-matching these methods (we do it ourselves), but we do have some tips for doing so successfully. First and foremost, try to that auto-indexing is disabled on repositories that will receive manually configured uploads. This can be done by ensuring that no [auto-indexing policies](/code-search/code-navigation/auto_indexing#configure-auto-indexing-policies) exist for the target repo. Even if one is not created explicitly for this repo, it may still be covered under a global policy with a matching repository pattern. If covering policies can't be changed to exclude a repo (or if you want _partially_ auto-indexed coverage), then the repository can be [explicitly configured](/code-search/code-navigation/auto_indexing#explicit-index-job-configuration) with a set of auto-indexing jobs to schedule. Simply delete the job configuration that conflicts the explicitly configured project (the directory and indexer's target language should match). Once an explicit set of jobs are configured, auto-inference will not update it. If the repository shape changes frequently (new index targets are added or removed). Therefore, this configuration should be manually refreshed as necessary. # Adding precise code navigation to CI/CD workflows ## Language-specific guides We are working on creating language specific guides for use with different indexers, so make sure to check for the documentation for your language! If there isn't a guide for your language, this general guide will help you through the precise code navigation setup process. ## Benefits of CI integration Setting up a source code indexing job in your CI build provides you with fast code navigation that gives you more control on when source code gets indexed, and ensures accuracy of your code navigation by keeping in sync with changes in your repository. Due to the large number of CI frameworks and languages we may not have specific documentation for your use case. Feel free to [contact us](https://help.sourcegraph.com/hc/en-us/requests/new) if you're having difficulties and we can help troubleshoot your setup. ## Using indexer containers We're working on creating containers that bundle indexers and the [Sourcegraph CLI (`src`)](https://github.com/sourcegraph/src-cli). All the languages in our [language-specific guides](/code-search/code-navigation/precise_code_navigation#setting-up-code-navigation-for-your-codebase) are supported, and this section provides a general guide to using those containers. We've pinned the containers to the `latest` tag in these samples so they stay up to date, but make sure you pin them before going live to ensure reproducibility. ### Basic usage Some indexers will work out of the box by just running them in your project root. Here's an example using GitHub actions and Go: ```yaml jobs: scip-go: #this line will prevent forks of this repo from uploading lsif indexes if: github.repository == '' runs-on: ubuntu-latest container: sourcegraph/scip-go:latest steps: - uses: actions/checkout@v1 - name: Generate index run: scip-go - name: Install src CLI run: | curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src - name: Upload index # this will upload to Sourcegraph.com, you may need to substitute a different command # by default, we ignore failures to avoid disrupting CI pipelines with non-critical errors. run: src code-intel upload -github-token=${{ secrets.GITHUB_TOKEN }} -ignore-upload-failure ``` ### Sub-projects If your repository contains multiple code projects in different folders, your CI framework likely provides a "working directory" option so that you can do something like: ```yaml jobs: scip-go: # Prevent forks of this repo from uploading SCIP indexes if: github.repository == '' runs-on: ubuntu-latest container: sourcegraph/scip-go:latest steps: - uses: actions/checkout@v1 - name: Generate index working-directory: backend/ run: scip-go - name: Install src CLI working-directory: backend/ run: | curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /usr/local/bin/src chmod +x /usr/local/bin/src - name: Upload index # note that the upload command also needs to happen in the same directory! working-directory: backend/ # this will upload to Sourcegraph.com, you may need to substitute a different command # by default, we ignore failures to avoid disrupting CI pipelines with non-critical errors. run: src code-intel upload -github-token=${{ secrets.GITHUB_TOKEN }} -ignore-upload-failure ``` ### Custom build environments Depending on which language you're using, you may need to perform some pre-indexing steps in a custom build environment. You have a couple options for this: - Add the indexer and [Sourcegraph CLI (`src`)](https://github.com/sourcegraph/src-cli) to your environment. To make this easier, you could either directly build a Docker image from our indexer container, or use its Dockerfile as inspiration for getting the tools installed in your environment. - Do the pre-indexing steps in your environment, and then make those artifacts available to our container. This second step is easy in GitHub actions because our container can be used as an action. Here's an example for a TypeScript project: ```yaml jobs: scip-typescript: # this line will prevent forks of this repo from uploading scip indexes if: github.repository == '' runs-on: ubuntu-latest container: my-awesome-container steps: - uses: actions/checkout@v1 - name: Install dependencies run: - name: Generate index uses: docker://sourcegraph/scip-typescript:latest with: args: scip-typescript index - name: Upload index uses: docker://sourcegraph/src-cli:latest with: # this will upload to Sourcegraph.com, you may need to substitute a different command # by default, we ignore failures to avoid disrupting CI pipelines with non-critical errors. args: src code-intel upload -github-token=${{ secrets.GITHUB_TOKEN }} -ignore-upload-failure ``` ### GitHub Action Examples The following projects have example GitHub Action workflows to generate and upload indexes. - [elastic/kibana](https://github.com/sourcegraph-codeintel-showcase/kibana/blob/7ed559df0e2036487ae6d606e9ffa29d90d49e38/.github/workflows/lsif.yml) - [golang/go](https://github.com/sourcegraph-codeintel-showcase/go/blob/master/.github/workflows/scip.yml) - [kubernetes/kubernetes](https://github.com/sourcegraph-codeintel-showcase/kubernetes/blob/359b6469d85cc7cd4f6634e50651633eefeaea4e/.github/workflows/lsif.yml) - [lodash/lodash](https://github.com/sourcegraph-codeintel-showcase/lodash/blob/b90ea221bd1b1e036f2dfcd199a2327883f9451f/.github/workflows/lsif.yml) - [moby/moby](https://github.com/sourcegraph-codeintel-showcase/moby/blob/380429abb05846de773d5aa07de052f40c9e8208/.github/workflows/lsif.yml) - [ReactiveX/IxJS](https://github.com/sourcegraph-codeintel-showcase/IxJS/blob/e53d323314043afb016b6deceaeb068d8d23c303/.github/workflows/lsif.yml) ### Circle CI Examples Certain frameworks like Circle CI may require you to explicitly cache artifacts between jobs. In CircleCI this might look like the following: ```yaml version: 2.1 jobs: install-deps: docker: - image: my-awesome-container steps: - checkout - - save_cache: paths: - node_modules key: dependencies jobs: scip-typescript: docker: - image: sourcegraph/scip-typescript:latest steps: - checkout - restore_cache: keys: - dependencies - run: scip-typescript index # this will upload to Sourcegraph.com, you may need to substitute a different command # by default, we ignore failures to avoid disrupting CI pipelines with non-critical errors. - run: src code-intel upload -github-token=<> -ignore-upload-failure workflows: scip-typescript: jobs: - install-deps - scip-typescript: requires: - install-deps ``` The following projects have example CircleCI configurations to generate and upload indexes. - [angular/angular](https://github.com/sourcegraph-codeintel-showcase/angular/blob/f06eec98cadab2ff7a1cef2a03ba7c42015eb399/.circleci/config.yml) - [facebook/jest](https://github.com/sourcegraph-codeintel-showcase/jest/blob/b781fa2b6683f04324edbc4b41552a94f97cd479/.circleci/config.yml) - [facebook/react](https://github.com/sourcegraph-codeintel-showcase/react/blob/e488420f686b88803cfb1bb09bbc4d3991db8c55/.circleci/config.yml) - [grafana](https://github.com/sourcegraph-codeintel-showcase/grafana/blob/664a694955ea40575a1cffe9db47a7adf4d3c2bb/.circleci/config.yml) - [helm](https://github.com/sourcegraph-codeintel-showcase/helm/blob/62c38f152d0802719aad1ec4c1c281f01dc75173/.circleci/config.yml) - [prometheus/prometheus](https://github.com/sourcegraph-codeintel-showcase/prometheus/blob/a0a8a249fff9d1c6ce4c097ccc4f5e120c723c51/.circleci/config.yml) - [ReactiveX/rxjs](https://github.com/sourcegraph-codeintel-showcase/rxjs/blob/c9d3c1a76a68273863fc59075a71b4cc43c06114/.circleci/config.yml) ### Travis CI Examples The following projects have example Travis CI configurations to generate and upload indexes. - [aws/aws-sdk-go](https://github.com/sourcegraph-codeintel-showcase/aws-sdk-go/blob/92f67a061fcdd46d6a418b28838b10b6ac63a880/.travis.yml) - [etcd-io/etcd](https://github.com/sourcegraph-codeintel-showcase/etcd/blob/eae726706fe8ebf7e08b45ba29a70388595db31b/.travis.yml) - [expressjs/express](https://github.com/sourcegraph-codeintel-showcase/express/blob/bd1ae153f19656183257ed223d518aeb9f5091ec/.travis.yml) - [hugo/hugo](https://github.com/sourcegraph-codeintel-showcase/hugo/blob/6704b7c125d7b21ccf2048d7bff0f1ffe2b0867d/.travis.yml) - [Microsoft/TypeScript](https://github.com/sourcegraph-codeintel-showcase/TypeScript/blob/f37f1dee1b3e63b12df2935590c8707a5ec3993b/.travis.yml) - [moment/moment](https://github.com/sourcegraph-codeintel-showcase/moment/blob/eedccdc2c07fb5abe931b427d50f5b3c3f44ac95/.travis.yml) - [sindresorhus/got](https://github.com/sourcegraph-codeintel-showcase/got/blob/164d55a029512cea7f245de870cbb1eaba114734/.travis.yml) ## CI from scratch If you're indexing a language we haven't documented yet in our [language-specific guides](/code-search/code-navigation/precise_code_navigation#setting-up-code-navigation-for-your-codebase), follow the instructions in this section. We want to have containers available for every language with a robust indexer, so please [contact](https://help.sourcegraph.com/hc/en-us/requests/new) to let us know we're missing one. ### Set up your CI machines Your CI machines will need two command-line tools installed. Depending on your build system setup, you can do this as part of the CI step, or you can add it directly to your CI machines for use by the build. 1. The [Sourcegraph CLI (`src`)](https://github.com/sourcegraph/src-cli). 2. The [indexer](/code-search/code-navigation/writing_an_indexer) for your language. ### Add steps to your CI 1. **Generate an index** for a project within your repository by running the indexer in the project directory (consult your indexer's docs). 1. **[Upload that generated index](/code-search/code-navigation/precise_code_navigation#setting-up-code-navigation-for-your-codebase)** to your Sourcegraph instance. ## Recommended upload frequency We recommend that you start with a CI job that runs on every commit (including branches). If you see too much load on your CI, your Sourcegraph instance, or a rapid decrease in free disk space on your Sourcegraph instance, you can instead index only the default branch, or set up a periodic job (e.g. daily) in CI that indexes your default branch. With periodic jobs, you should still receive precise code navigation on non-indexed commits on lines that are unchanged since the nearest indexed commit. This requires that the indexed commit be a direct ancestor or descendant no more than [100 commits](https://github.com/sourcegraph/sourcegraph-public-snapshot/blob/e7803474dbac8021e93ae2af930269045aece079/lsif/src/shared/constants.ts#L25) away. If your commit frequency is too high and your index frequency is too low, you may find commits with no precise code navigation at all. In this case, we recommend you try to increase your index frequency if possible. ## Uploading indexes to Sourcegraph.com Indexes can be uploaded to a self-hosted Sourcegraph instance or to [Sourcegraph.com](https://sourcegraph.com). Using the [Sourcegraph.com](https://sourcegraph.com) endpoint will surface code navigation for your public repositories directly on GitHub via the [Sourcegraph browser extension](/integration/browser_extension) and at `https://sourcegraph.com/github.com//`. Using the [Sourcegraph.com](https://sourcegraph.com) endpoint is free and your index is treated as User-Generated Content (you own it, as covered in our [Sourcegraph.com terms of service](https://about.sourcegraph.com/terms-dotcom#3-proprietary-rights-and-licenses)). If you run into trouble, or a situation arises where you need all of your index expunged, please reach out to us at [support@sourcegraph.com](mailto:support@sourcegraph.com). # Code Graph Data uploads

How Code Graph indexers analyze code and generate and index file.

[Code graph indexers](/code-search/code-navigation/writing_an_indexer) analyze source code and generate an index file, which is subsequently [uploaded to a Sourcegraph instance](/code-search/code-navigation/how-to/index_other_languages#upload-scip-data) using [Sourcegraph CLI](/cli/) for processing. Once processed, this data becomes available for [precise code navigation queries](/code-search/code-navigation/precise_code_navigation). ## Lifecycle of an upload Uploaded index files are processed asynchronously from a queue. Each upload has an attached `state` that can change as work associated with that data is performed. The following diagram shows the possible transition paths from one `state` of an upload to another. ![Upload state diagram](https://storage.googleapis.com/sourcegraph-assets/Docs/upload-states.svg) The typical sequence for a successful upload is: `UPLOADING_INDEX`, `QUEUED_FOR_PROCESSING`, `PROCESSING`, and `COMPLETED`. In some cases, the processing of an index file may fail due to issues such as malformed input or transient network errors. When this happens, an upload enters the `PROCESSING_ERRORED` state. Such error uploads may undergo multiple retry attempts before moving into a permanent error state. At any point, an uploaded record may be deleted. This can happen due to various reasons, such as being replaced by a newer upload, due to the age of the upload record, or by explicit deletion initiated by the user. When deleting a record that could be used for code navigation queries, it transitions first into the `DELETING` state. This temporary state allows Sourcegraph to manage the set of Code Graph uploads smoothly. Changing the state of an upload to or from `COMPLETED` requires updating the [repository commit graph](#repository-commit-graph). This process can be computationally expensive for the worker service or Postgres database. ## Lifecycle of an upload (via UI) After successfully uploading an index file, the Sourcegraph CLI will provide a URL on the target instance to track the progress of that upload. Uploading a code graph index via the Sourcegraph CLI You can view the data uploads for a specific repository by navigating to the Code Graph page on the target repository's index page. Repository index page Alternatively, website administrators of a Sourcegraph instance can access a global view of Code Graph data uploads across all repositories from the **Site Admin** page. Global list of code graphd data uploads across all repositories ## Repository commit graph Sourcegraph maintains a mapping from a commit of a repository to the set of upload records that can resolve a query for that commit. When an upload record transitions to or from the `COMPLETED` state, the set of eligible uploads changes, and this mapping must be recalculated. Upon a state change in an upload, we flag the repository as needing an update. Subsequently, the worker service updates the commit graph and asynchronously clears the flag for that repository. When an upload changes state, the repository is flagged as requiring an update status. Then the [`worker` service](/admin/workers#codeintel-commitgraph) will update the commit graph and unset the flag for that repository asynchronously. While this flag is set, the repository's commit graph is considered `stale`. This means there may be some upload records in a `COMPLETED` state that aren't yet used to resolve code navigation queries. The state of a repository's commit graph can be seen in the code graph data page on the target repository's index page. Stale repository commit graph notice Once the commit graph has updated (and no subsequent changes to that repository's uploads have occurred), the repository commit graph is no longer considered `stale`. Up-to-date repository commit graph notice
# Inference of auto-indexing jobs This feature is in beta for self-hosted customers. When a commit of a repository is selected as a candidate for [auto-indexing](/code-search/code-navigation/auto_indexing) but does not have an explicitly supplied index job configuration, index jobs are inferred from the content of the repository at that commit. The site-config setting `codeIntelAutoIndexing.indexerMap` can be used to update the indexer image that is (globally) used on inferred jobs. For example, `"codeIntelAutoIndexing.indexerMap": {"go": "scip-go:alternative-tag"}` will cause inferred jobs indexing Go code to use the specified container (with an alternative tag). This can also be useful for specifying alternative Docker registries. This document describes the heuristics used to determine the set of index jobs to schedule. See [configuration reference](/code-search/code-navigation/auto_indexing_configuration) for additional documentation on how index jobs are configured. As a general rule of thumb, an indexer can be invoked successfully if the source code to index can be compiled successfully. The heuristics below attempt to cover the common cases of dependency resolution, but may not be sufficient if the target code requires additional steps such as code generation, header file linking, or installation of system dependencies to compile from a fresh clone of the repository. For such cases, we recommend using the inferred job as a starting point to [explicitly supply index job configuration](/code-search/code-navigation/auto_indexing#explicit-index-job-configuration). ## Go For each directory containing a `go.mod` file, the following index job is scheduled. ```json { "index_jobs": [ { "steps": [ { "root": "", "image": "sourcegraph/scip-go", "commands": [ "go mod download" ] } ], "root": "", "indexer": "sourcegraph/scip-go", "indexer_args": [ "scip-go", "--verbose" ] } ] } ``` For every _other_ directory excluding `vendor/` directories and their children containing one or more `*.go` files, the following index job is scheduled. ```json { "root": "", "indexer": "sourcegraph/scip-go", "indexer_args": [ "GO111MODULE=off", "scip-go", "--verbose" ] } ``` ## TypeScript For each directory excluding `node_modules/` directories and their children containing a `tsconfig.json` file, the following index job is scheduled. Note that there are a dynamic number of pre-indexing steps used to resolve dependencies: for each ancestor directory `ancestor(dir)` containing a `package.json` file, the dependencies are installed via either `yarn` or `npm`. These steps run in order, depth-first. ```json { "steps": [ { "root": "", "image": "sourcegraph/scip-typescript:autoindex", "commands": [ "yarn" ] }, { "root": "", "image": "sourcegraph/scip-typescript:autoindex", "commands": [ "npm install" ] }, "..." ], "local_steps": [ "N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release n --arch x64-musl autol" ], "root": "", "indexer": "sourcegraph/scip-typescript:autoindex", "indexer_args": [ "scip-typescript", "index" ] } ``` ## Rust If the repository contains a `Cargo.toml` file, the following index job is scheduled. ```json { "root": "", "indexer": "sourcegraph/lsif-rust", "indexer_args": [ "lsif-rust", "index" ], "outfile": "dump.lsif" } ``` ## Java Inference for languages supported by [scip-java](https://github.com/sourcegraph/scip-java) is currently restricted to Sourcegraph.com. If the repository contains both a `lsif-java.json` file as well as `*.java`, `*.scala`, or `*.kt` files, the following index job is scheduled. ```json { "root": "", "indexer": "sourcegraph/scip-java", "indexer_args": [ "scip-java", "index", "--build-tool=lsif" ], "outfile": "index.scip" } ``` # Private Resources on on-prem data center via Sourcegraph Connect agent This feature is in the Experimental stage. Please contact Sourcegraph directly via [preferred contact method](https://about.sourcegraph.com/contact) for more information. As part of the [Enterprise tier](https://sourcegraph.com/pricing), Sourcegraph Cloud supports connecting private resources on any on-prem private network by running Sourcegraph Connect tunnel agent in customer infrastructure. ## How it works Sourcegraph will set up a tunnel server in a customer dedicated GCP project. Customer will start the tunnel agent provided by Sourcegraph with the provided credential. After start, the agent will authenticate and establish a secure connection with Sourcegraph tunnel server. Sourcegraph Connect consists of three major components: Tunnel agent: deployed inside the customer network, which uses its own identity and encrypts traffic between the customer code host and client. Agent can only communicate with permitted customer code hosts inside the customer network. Only agents are allowed to establish secure connections with tunnel server, the server can only accept connections if agent identity is approved. Tunnel server: a centralized broker between client and agent managed by Sourcegraph. Its purpose is to set up mTLS, proxy encrypted traffic between clients and agents and enforce ACL. Tunnel client: forward proxy clients managed by sourcegraph. Every client has its own identity and it cannot establish a direct connection with the customer agent, and has to go through tunnel server. [link](https://link.excalidraw.com/readonly/453uvY8infI8wskSecGJ)