[SERVER-19710] shellPrintHelper() and shellPrint() printing conventions not well defined Created: 01/Aug/15  Updated: 06/Dec/22  Resolved: 03/Dec/21

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Kevin Pulo Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 0
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-19683 Shell inserts no longer display retur... Closed
is related to SERVER-19791 Save mongo shell session into a file ... Closed
Assigned Teams:
Server Tooling & Methods
Operating System: ALL
Participants:

 Description   

shellPrintHelper() usually calls print(), but in the case of an object with a shellPrint() function it instead returns the result of calling shellPrint().

Sometimes (often?) shellPrint() calls print() (eg. DBQuery), but sometimes it returns a string (eg. WriteResult). In this latter case, the printing happens via printResult = true being passed to ImplScope::exec() when shellPrintHelper() is run.

This is all a bit confusing and inconsistent. We should settle on print()ing conventions for shellPrint() and shellPrintHelper(), and then audit all uses to make them compliant. The possible options are:

  1. shellPrint() and shellPrintHelper() always call print() in their dynamic scope, and never return any value.
  2. shellPrint() and shellPrintHelper() never call print() in their dynamic scope, and always return the string to be printed. This is then actually printed via scope->exec()/printResult (as above).

I'm in favour of the latter, since it allows the exact shell output to be determined programmatically (and potentially tested in jstests) by the expression "shellPrintHelper(foo)", rather than having to mess around capturing print() calls ala SERVER-19368.



 Comments   
Comment by Brooke Miller [ 03/Dec/21 ]

We've deprecated the mongo shell in favor of the new[ mongosh|https://docs.mongodb.com/mongodb-shell/]. Unfortunately, we aren't able to pursue improvements to the deprecated shell except in extreme cases, such as critical security fixes. Please start making use of mongosh and let us know if it works for you in this case.

Comment by Kevin Pulo [ 02/Mar/18 ]

We keep adding shellPrint() methods which return strings instead of printing, most recently in SessionOptions and DriverSession. These don't work when manually passed to the global shellPrint() function (but they would if they printed and returned undefined). The other places I could find which also do this are:

  • MinKey and MaxKey handling inside shellPrintHelper() itself

    > MinKey
    { "$minKey" : 1 }
    > shellPrint(MinKey)
    > print(MinKey)
    [object MinKey]
    >
    

  • WriteConcern, WriteResult, BulkWriteResult, WriteCommandError, WriteError, WriteConcernError (all in bulk_api.js)
  • Explainable (in explainable.js)
  • DBCollection, PlanCache (in collection.js)
  • DBExplainQuery (in explain_query.js)

The workaround is usually just to call print(foo.shellPrint()) instead, but it's annoying to have to remember/figure out when this is necessary, and when it isn't or won't work.

Generated at Thu Feb 08 03:51:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.