Potential False Positive - TypeScript Rule S4123 with Mongoose 6.x


  • ALM: GitHub
  • CI system: GitHub Actions
  • Languages of the repository: TypeScript, JavaScript, CSS
  • Steps to reproduce - See code snippets below, using Mongoose 6.x and analysing with the latest SonarCloud GitHub Action.
  • Potential workarounds - See below
  • TypeScript Rule ID: S4123

When using SonarCloud to analyse our codebase, we observe what appears to be a significant false-positive when analysing asynchronous behaviour of Queries in the Mongoose library (we use v6.x).

Consider the following (admittedly slightly contrived) example. We Create a new Mongoose Model like so,

// src/models/example.ts

import mongoose, { Model, Schema } from 'mongoose';
import { ExampleDocument } from 'src/common/interfaces/example';

const exampleSchema = new Schema<ExampleDocument>({
  name: string,
});

export const ExampleModel: Model<ExampleDocument> =  mongoose.model('Example', exampleSchema);

When using the model elsewhere in the code, like so,

import { ExampleModel } from 'src/models/example';

export const getExampleByName = async (name: string) => {
  const exampleDocument = await ExampleModel.findOne({ name }, 'name');
  if (!exampleDocument) throw new Error(`Example document not found for ${name}`);
  return exampleDocument;
};

The code await ExampleModel.findOne({ name }, 'name'); is marked as a violation of TypeScript rule S4123, with the message “Refactor this redundant ‘await’ on a non-promise.”
While this is strictly correct, all the return type of Model.findOne() in Mongoose is a Mongoose Query, which while not a true spec-compliant Promise, is a thenable, as it contains a .then() method. This makes it entirely valid syntax to use await with the query.

Have noted the discussions here and here, as well as the linked GitHub Issue in SonarJS here. Either this issue is a regression, or the issue has not been fixed for thenables.

We have considered the following workarounds for our use case, but feel this should reported a false-positive for investigation by you folks.

  • Disabling the rule S4123 for our repository - not deemed appropriate as it will lead to real positives not being detected.
  • Chaining our Mongoose queries with .exec() as described here in the Mongoose docs. While this would work to suppress the Sonar issue, it goes against our internal coding style.
  • Manually marking every occurrence in our code base as a false positive. While this is possible, it is tedious given the number of occasions this occurs in our codebase.

Thanks in advance for your support, and any additional tips the community might have for us!

1 Like

Hello Jonny Squire,

We’ve opened a ticket for this issue: Fix FP S4123 (`no-invalid-await`): don't flag when function returns a thenable object · Issue #4113 · SonarSource/SonarJS · GitHub

Best,
Ilia

1 Like

Awesome stuff :star_struck:

Thanks to everyone for working to fix this!

Look forward to seeing this deployed to SonarCloud soon!

[A previous version of this comment referenced a drop in bugs. This was an unrelated configuration change we made internally. This Rule S4123 is a Code Smell, not a Bug. And given SonarJS is not yet in SonarCloud, that was jumping the gun a bit! Sorry!]

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.